URL Rewrite to change querystring case - asp.net

I am looking to setup a URL rewrite rule in my web.config that will modify the following URL:
/detail.aspx?aID=164&mode=t
To (see case of aid):
/detail.aspx?aid=164&mode=t
Please can anyone assist me with this? The only other thing to mention here is that the rule should still work if there is no mode parameter at the end and irrespective of what order the aid parameter appears in the querystring.
EDIT 1
I found this guide which rewrites the whole URL to lowercase. This would work for me only the accepted solution seems to ignore the query string values.
How to display URL in lower case?
EDIT 2
I'm now using the following to issue a 301 redirect when uppercase characters are found. the accepted answer addresses the original question but this solution works on the full URI, domain, path and querystring.
'301 REDIRECT ON UPPERCASE URIS
Dim fullUri As String = Request.Url.AbsoluteUri
If fullUri.Any(Function(c) Char.IsUpper(c)) Then
Response.RedirectPermanent(fullUri.ToLower)
End If

EDIT:
You are right, did not realize it was same page.
You need to add another condition.
<rule name="URL Lower" enabled="true" stopProcessing="true">
<match url="^(detail.aspx?)(.*)" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(.*)" />
<add input="{QUERY_STRING}" pattern="([A-Z]+)" ignoreCase="false" />
</conditions>
<action type="Redirect" url="detail.aspx?{ToLower:{C:1}}" appendQueryString="false" />
</rule>
Examples:
/detail.aspx?aID=164&mode=t
converts to
/detail.aspx?aid=164&mode=t
and /detail.aspx?aid=164&mode=t is ignored because of second rule.

Related

Regex to redirect a url using url rewrite

OK, I'm wondering if someone can lend a hand with a regex I'm trying to write.
Basically, what I want to do is use IIS urlrewrite module to do a redirect to a specific URL if the user accesses a URL on another site. The only catch is I have to also capture a bit of the query string, and move it into the redirect.
so here is the input, the URL that a user may access would look like:
https://of.example.com/sfsv3.aspx?waform=pro&language=en
I want to match that URL (either http or https, case insensitive), and capture from it also one piece of information, the two letter language code. then the url i want to forward the user to looks like:
http://example.com/ca/en/ppf
(where en is replaced by whatever i captured above)
So, I'm working with IIS Rewrite module, and I've gotten my input data and regex in, so far the regex pattern I have is this:
https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))
so basically i'm matching the whole string, plus a group and a subgroup for language and it's code. in the IIS test pattern dialog, this is working.
I get the following
{R:1} language=en
{R:2} en
great! so then my IIS rewrite rule should look like this to redirect the user:
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true" enabled="true">
<match url="https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))" ignoreCase="true" />
<action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
</rule>
</rules>
</rewrite>
</system.webServer>
this all seems right to me. however, the redirect is not occurring. it seems to have a problem with the part \? (an escaped question mark to mark the start of the query string). if this is included, then the redirect simply does not happen.
Can you help me figure out how to get it work?
for the record, I figured it out. this is a special case of regex, running it inside a web.config as part of a urlrewrite action. in that case, you can't handle the query string with simple regex, you have to put in conditions on the query string. Here's what eventually ended up working:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect" stopProcessing="true" stopProcessing="true" enabled="true">
<match url="sfsv3.aspx\.aspx" ignoreCase="true"/>
<conditions>
<add input="{QUERY_STRING}" pattern="subject=PROPRCH" />
<add input="{QUERY_STRING}" pattern="(..)/subject" />
</conditions>
<action type="Redirect" url="https://www.example.com/ca/{C:1}/forms/ppf" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>

Url Rewrite Regex to Match Some Classic ASP Files (But not all)

I have a bunch of old .asp files that I'm trying to redirect. I do not want to redirect all .asp files, just ones related to the "News" section.
Examples:
news_1-4-2009.asp
news_2-15-2008.asp
news.asp
I created a redirect rule that almost works:
<rule name="NewsRedirect" stopProcessing="true">
<match url=".*news(.*asp)?" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" appendQueryString="false" url="http://www.example.com/about/newsroom" redirectType="Permanent" />
</rule>
The problem (as you might see) is that the action url is also a match and creates an infinite loop.
Question
I'm trying to say...
Any file that starts with "news" and ends in ".asp".
Thanks for any help!
Any file that starts with "news" and ends in ".asp".
Give this a try:
^news.*\.asp$

IIS URL rewrite module url's to lowercase

For better SEO we are using URL rewrite to convert all the URL's to lowercase. I set this one as mentioned in this the below article.
Everything is working fine from URL perspective, but we see lot of 301 redirects when we check in fiddler.
It looks like the images, javascript, css, jquery ajax calls and everything is getting converted into lower case.
I am trying to remove that and want to rewrite only aspx extension and no extension urls. I tried to play around the matchurl without any success. Any help or guidelines will be highly appricated.
Thanks
Edit:
My Current rule is
<rules>
<rulename="LowerCaseRule1"patternSyntax="ExactMatch"stopProcessing="true">
<matchurl="[A-Z]"ignoreCase="false"/>
<actiontype="Redirect"url="{ToLower:{URL}}"/>
</rule>
</rules>
You could probably use something as follow:
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="\.aspx$" />
<add input="{REQUEST_FILENAME}" pattern="\." negate="true" />
</conditions>
</rule>
The rule will be triggered only if one of the condition is true:
The first one checks if the requested path (filename) ends with .aspx.
The second one checks if the if the requested path (filename) doesn't contain a . (so doesn't have an extension)

IIS7 url rewrite issue when having "?"

I have a URL in the format
abc/pqr/xyz/?word1
and this needs to redirect to
abc/pqr/xyz/?word2
Is is possible to do using IIS7 rewrite?
It is possible using the IIS7 rewrite module with the following rule:
<rule name="Rewrite querystring" stopProcessing="true">
<match url="^abc/pqr/xyz/?$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^word1$" />
</conditions>
<action type="Redirect" url="{R:0}?word2" appendQueryString="false" />
</rule>
What it does is: check that the url is abc/pqr/xyz/ or abc/pqr/xyz and the query string exactly word1. If yes, it redirects the user to the same url ({R:0}) but appending ?word2 instead.
It is important to have the appendQueryString="false" option as you don't want the module to append your word1 at the end.
By default, if not specified, the redirect is a 301 (permanent), that, regarding #Owen comment, seems to be the best fit for your case!
See here
You need to specify the url to match, " abc/pqr/xyz", then the query string to match and replace.

IIS7 Url Rewrite not being used

I'm rebuilding part of an existing asp application, the new parts are build with MVC.
One of the old screens which I'm not touching at the moment needs to point to a new location. For that reason I've set up a URL redirect in IIS on machine level with the following regular expression:
^workflows/screen\.asp\?objectid=([0-9]+)(.*)
When using the tester I can see that it's a correct expression which catches the necessary items and I need to send the users to the following new URL:
http://domainname/newapppart/select/{R:1}
When testing it in my browser though it seems as if the rewrite rule is not even in place as I'm still send to the old page.
Query string is not included in main match string, you have to use Conditions to evaluate it.
<rule name="MyRule" stopProcessing="true">
<match url="^workflows/screen\.asp$" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="objectid=([0-9]+).*" />
</conditions>
<action type="Rewrite" url="http://domainname/newapppart/select/{C:1}" appendQueryString="false" />
</rule>

Resources