Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL Rewrite module repeats query string

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?

like image 515
mohammad rostami siahgeli Avatar asked Jan 26 '18 07:01

mohammad rostami siahgeli


People also ask

How does IIS URL Rewrite work?

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.

What is IIS URL Rewrite Module?

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.

What is the difference between redirect and rewrite?

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.

What is Request_uri in URL Rewrite?

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.


1 Answers

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>
like image 50
Victor Leontyev Avatar answered Oct 17 '22 03:10

Victor Leontyev