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

烈酒焚心 提交于 2019-12-18 16:51:18

问题


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!


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/25767014/iis-rewrite-rule-in-web-config-to-redirect-https-requests-to-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!