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?
The URL rewriting module runs early in the request-processing pipeline, modifying the requested URL before the Web server decides which handler to use to process the request. The handler, which is chosen based on the rewritten URL, processes the request and generates a response that is sent back to the Web browser.
About the URL Rewrite module The Microsoft URL Rewrite Module 2.0 for IIS 7 and above enables IIS administrators to create powerful customized rules to map request URLs to friendly URLs that are easier for users to remember and easier for search engines to find.
Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.
These server variables can be accessed by using a condition within a rule. The server variable REQUEST_URI can be used to access the entire requested URL path, including the query string.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With