Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS Rewrite Rule in web.config to redirect HTTPS requests to HTTP

I need to redirect all https requests to http, for example, if someone visits https://www.example.com/another-page/ to http://www.example.com/another-page/

I have the following rewrite rule in my web.config right now, but it's not working correctly. It's redirecting https://www.example.com/another-page/ to https://www.example.com/, so to the root of the site, but instead I want the redirect to stay in the same URL and only rewrite https to http.

 <rule name="Redirect to HTTP" stopProcessing="true">
   <match url="(.*)" />
     <conditions>
       <add input="{R:1}" pattern="^onepage/(.*)$" negate="true" />
       <add input="{HTTPS}" pattern="^ON$" />
     </conditions>
     <action type="Redirect" url="http://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
 </rule>

Any help on changing the above rule so that it only changes https to http, but keeps the full url visited would be greatly appreciated!

like image 869
IntricatePixels Avatar asked Sep 10 '14 13:09

IntricatePixels


People also ask

How do I redirect HTTPS request to HTTP in IIS?

Without considering the security of your website, just remove the Https binding and add an Http binding in the site binding module in IIS. The website will work only over the HTTP protocol. Besides, Also, IIS URL Rewrite Extension is another choice to achieve this. Install the IIS URL Rewrite Extension .

How do I create a redirect rule in IIS?

Set the Redirect URL to https://{HTTP_HOST}/{REQUEST_URI}, which will redirect the user to the same page path (using server variable REQUEST_URI) under the same domain (or “host,” using server variable HTTP_HOST), but using HTTPS as the protocol.


2 Answers

I set up your rule, cleaned up a little, and it worked; so this isn't really answering with much new.

Suggestion: Remove the onepage input condition just for testing, as cheesmacfly suggested in the question comment.

Also, try changing the action to {R:1} instead of {R:0}. It shouldn't matter in this case, but I just like using 1 and up, to match the specific capturing group. R:0 means the entire matched string, which always confuses me just a little.

<rule name="Redirect to HTTP" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTPS}" pattern="^ON$" />
    </conditions>
    <action type="Redirect" url="http://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>

One possibility is that your browser has cached a previous attempt of your rules. When the redirectType is Permanent, and you're still developing or testing, the browser often caches a previous rule. Clear your browser cache, and/or remove the Permanent, and/or browse in incognito mode. When done testing, change it to permanent. See number 2 and 3 in this answer: https://stackoverflow.com/a/9204355/292060

like image 194
goodeye Avatar answered Oct 02 '22 22:10

goodeye


Please paste the below code in web.config file.

<rule name="Redirect to http" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP}" pattern="off" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="http://{HTTPS_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
like image 41
Anoop Asok Avatar answered Oct 02 '22 21:10

Anoop Asok