IIS7 url rewrite issue when having "?" - iis-7

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.

Related

How to remove querystring when using Umbraco rewrite

I have a rewrite rule as below to my Umbraco/ASP .Net site
<add name="Test" virtualUrl="^~/category/(.*)" destinationUrl="/category?cat=$1" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
The idea is if a user types
www.example.com/category/electronics
www.example.com/category/devices
www.example.com/category/food
www.example.com/category/beverages
Everything works as i would expect when they reach the page without any rules and plain URLs i.e.
www.example.com/category?cat=electronics
If i have the above rule it enabled it always adds/forwards the querystring parameter i.e cat= when you click a link on any of the pages above.
How could i stop forwarding/adding the parameter?
You could use below URL rewrite rule:
<rule name="add query string" stopProcessing="true">
<match url="^category/([^/]+)/$" />
<action type="Redirect" url="http://www.sample1.com/category?cat={R:1}" appendQueryString="false" />
</rule>
if you did not install URL rite module in iis you could install it from the below link:
https://www.iis.net/downloads/microsoft/url-rewrite

ASP.NET redirect. Possible loop?

I want to redirect from all url that contains 'flashmediaelement.swf' to '/mediaplayer/flashmediaelement.swf'. Is it possible that there will be a loop and it will be redirecting without end?
I would use
<rule name="Redirect flashmediaelement" enabled="true" stopProcessing="true">
<match url="/flashmediaelement.swf$" />
<action type="Redirect" url="/mediaplayer/flashmediaelement.swf" />
</rule>
I want to redirect from any pages ending with "/flashmediaelement.swf" (like "mypage.com/blabla/blabla/flashmediaelement.swf" or "mypage.com/flashmediaelement.swf") to "mypage.com/mediaplayer/flashmediaelement.swf"
No , your regular expression ^/flashmediaelement.swf$ specifies exact match for url , so it won't match '/mediaplayer/flashmediaelement.swf
See first section here for exact match regex

URL Rewrite to change querystring case

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.

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)

Simple trailing slash URL rewrite

I've never done URL rewriting (redirecting). I have a website http://sub.sub.domain.ext/app/. "app" signifies an "Application", not a virtual directory.
When a user navigates to http://sub.sub.domain.ext/app (no slash) I need the IIS 7 to redirect him to the URL with the trailing slash.
The thing is that I want the rule to be applied only when the user navigates to application. I don't want the trailing slash to be appended to every filename.
I have tried modifying the predefined rule in IIS7 manager but with no success. I've tried exact matching the whole URL, constraining the conditions, or simply using the original predefined rule. But even when using the original rule, it rewrites all subsequent request files/dirs/URLs, but it does not redirect the user from http://sub.sub.domain.ext/app to http://sub.sub.domain.ext/app/.
The rule you are looking might be as simple as:
<rule name="Add trailing slash" stopProcessing="true">
<match url="^app$" negate="false" />
<action type="Redirect" url="{R:0}/" />
</rule>
The url="^app$" pattern matches only the url http://www.yourwebsite.com/app and nothing else.
If you need to limit this behavior to the host sub.sub.domain.ext, you can add a condition:
<rule name="test" stopProcessing="true">
<match url="^app$" negate="false" />
<action type="Redirect" url="{R:0}/" />
<conditions>
<add input="{HTTP_HOST}" pattern="^sub.sub.domain.ext$" />
</conditions>
</rule>

Resources