IIS URL Rewrite module repeats query string - url-rewrite-module

I have this rule, to redirect all HTTP traffic to HTTPS:
<rewrite>
<rules>
<rule name="RedirectToHttps" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
I found it on the Internet.
It works fine for simple URLs without query strings. But it duplicates query strings. For example http://example.com/path?key=value becomes https://example.com/path?key=value&key=value.
This causes problems in debugging and troubleshooting. What causes this behavior?

Problem is that variable REQUEST_URI contains URL and query string. In your case you have two solutions:
1) Add attribute appendQueryString="false" to your action. Rule should be like that:
.
<rule name="RedirectToHttps" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Found" />
</rule>
2) Instead REQUEST_URI you can use URL variable because this variable contains only URL without the query string. The rule should be like that:
.
<rule name="RedirectToHttps" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{URL}" redirectType="Found" />
</rule>

Related

Removing &PageSpeed=noscript from the end of URL IIS Rewrite

I am trying to remove the last part of the URL with IIS Rewrite, but somewhere I do the things wrong.
Example URL:
https://example.com/BGLog/pages/register/?ReturnUrl=/blog/arebemagare/site/posts/?bid=37780&PageSpeed=noscript
I need to remove &PageSpeed=noscript
I wroted rule, but it strips also ?ReturnUrl=/blog/arebemagare/site/posts/?bid=3778 :
<rule name="Remove paging parameters" stopProcessing="true">
<match url="(.*)?$" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(.*)(.*)&PageSpeed=noscript(.*)" />
</conditions>
<action type="Redirect" url="{R:1}" appendQueryString="false" redirectType="Temporary" />
Any suggestion? Thanks!
I think I've achieved it, but with two rules:
<rule name="Remove &PageSpeed" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(.*)(&PageSpeed=noscript)" />
</conditions>
<action type="Redirect" url="{R:1}?{C:1}" appendQueryString="false" redirectType="Found" />
</rule>
<rule name="Remove ?PageSpeed" enabled="true" stopProcessing="true">
<match url="(.*)?$" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)(PageSpeed=.+)(.*)" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
Please try the following ruleļ¼š
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)&(PageSpeed=noscript)" />
</conditions>
<action type="Redirect" url="{R:1}?{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
The result of my test is working, hope this works for you too.
It works! But I found it catches also https://example.com/BGLog/blog/filteredlist?cat=5021&PageSpeed=noscript and rewrites it to
https://example.com/BGLog/blog/filteredlist
I've tried to edit it to not strip ?cat=5021, but without success :(

Is this a properly formatted url rewritr rule?

I am trying to redirect the two links below to the root.
My question is can you see a problem with my rule written below?
Thank You!
http://pollen.aaaai.org/nab/collectors/
http://pollen.aaaai.org/nab/collectors/index.cfm?p=Count_form
<rule name="Redirect /nab/collectors/ to http://pollen.aaaai.org/" patternSyntax="Wildcard" stopProcessing="false">
<match url="*/nab/collectors/*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Rewrite" url="http://pollen.aaaai.org" appendQueryString="false" />
</rule>
There is a problem with your parameter, it matches the content after the hostname and does not contain "/". you can refer to below rule about redirect to root.
Note: I changed patternSyntax from wildcards to regular expressions.
<rule name="Redirect /nab/collectors/ to http://pollen.aaaai.org/" patternSyntax="Regular Expressions" stopProcessing="false">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Rewrite" url="http://pollen.aaaai.org" appendQueryString="false" />
</rule>

IIS RewriteRules

I have a RewriteRule that prepends "www" to the URL, granted that it does not already exist.
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
The trouble is, I have another inbound domain (www.example2.com) for which I do not want this rewrite rule to apply. I would think that the following would work fine.
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="www.example.com" negate="true" />
<add input="{HTTP_HOST}" pattern="www.example2.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
But it just seems to be ignored. Any suggestions? Thanks!
You can do it with this rule. It will add www, only for domain example.com and will ignore all other domains:
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
Victor got me most of the way there. Thanks!
<rule name="Add www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
patternSyntax has to be the default regular expression value. That means the URL pattern had to be altered, and the backtrack expression is zero-based.

URL rewrite between 2 urls that are being handled by the same web.config

I am getting a redirect loop when I try and implement this mobile redirect I created:
<rewrite>
<rules>
<rule name="Mobile Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="website.com.au" />
<add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
</conditions>
<action type="Redirect" url="http://mwebsite.com.au" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
This is the catch though, the mwebsite.com.au is assigned to the same web.config as website.com.au so they are handled by the same web.config. This is the way the .net application I am dealing with is handling the request (I cannot split them up, they have to pass through this 1 web.config)
I have tested this by replacing mwebsite.com.au with google.com.au and it works perfectly but for some reason URLREWRITE cannot process the request when it has to inject mwebsite.com.au back through the same rule.
any help would be amazing.
Your rule is basically saying: if {HTTP_HOST} contains website.com.au and {HTTP_USER_AGENT} contains any of midp, mobile or phone, redirect to http://mwebsite.com.au.
As you can guess, http://mwebsite.com.au contains website.com.au.
To fix this, simply tell your condition that it should START with website.com.au using the pattern ^website.com.au.
So your rule would become:
<rewrite>
<rules>
<rule name="Mobile Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^website.com.au" />
<add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
</conditions>
<action type="Redirect" url="http://mwebsite.com.au" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

IIS7 URL Rewrite module rule help

I'm trying to turn off HTTPS when someone hits the root of my site...
I want
https://www.domain.com/
to redirect to
http://www.domain.com/
but I also want..
https://www.domain.com/secure.aspx
or any other named page not to redirect.
Here's what I have so far but I can't get it to work. I tried a thousand ways, and read all the IIS documentation and examples on this, but I can't come up with the magic formula.
<rule name="Redirect Root Only to Non HTTP" stopProcessing="true">
<match url="\.com/$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="http://www.domain.com" appendQueryString="false" redirectType="Found" />
</rule>
I think this is your answer:
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^.*\.aspx$" ignoreCase="true" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTP" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^$" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/" redirectType="Permanent" />
</rule>
You were close .. except the actual pattern (which matches URL path part only and not domain name). The proper pattern is ^$ which means empty string which will ONLY match domain root hit e.g. https://www.domain.com/.
The full rule will be:
<rule name="Redirect Root to HTTP" stopProcessing="true">
<match url="^$"/>
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="on"/>
</conditions>
<action type="Redirect" url="http://www.domain.com/" redirectType="Permanent"/>
</rule>

Resources