IIS HTTP to HTTPS relative redirect

2020-03-01 07:23发布

I recently got a SSL certificate for my website and want to redirect all traffic to HTTPS. I got everything to go to https://mydomain.com but if someone enters http://mydomain.com/anotherpage it drops the other page and just takes the user to the home page.

My rule in my web.config file looks like this:

<rule name="HTTP to HTTPS redirect" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  </conditions>
  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>

I also tried https://{HTTP_HOST}{REQUEST_URI} without any success. Can anyone tell me what I need to do to make the website redirect to the proper HTTPS version of the page? I have a feeling it has something to do with the pattern, but I can't seem to figure out the syntax.

8条回答
来,给爷笑一个
2楼-- · 2020-03-01 08:01

You can add the URL Rewrite module to IIS (IIS 7 or higher) which allows you to add create the redirect in a visual way. The module can be downloaded here.

This step-by-step tutorial worked wonders for me and explains that when using this module, all it actually does is add some code to your web.config file as such:

<rewrite>
    <rules>
        <rule name="Redirect to HTTPS" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
        </rule>
    </rules>
</rewrite>
查看更多
可以哭但决不认输i
3楼-- · 2020-03-01 08:04

I can't comment yet or I'd leave this as a comment under AndyH's answer. The solution was correct, though I hit a single further snag (likely tied to the use of Adobe's Coldfusion server). I wanted to share some further research I had to do for any other unfortunate soul who may run into it.

Once set up, the redirect would always end at this url:

https://xxx.xxx.com/jakarta/isapi_redirect.dll

The fix for this was found in an Adobe thread (https://forums.adobe.com/thread/1034854): I had to change an application pool's settings as follows:

Real site (HTTPS binding only, actually contains code and virtual directories) Application pool's Advanced Settings: Enable 32-Bit Applications : False

Http_Redirect site (HTTP binding only, is a blank shell of a folder with no directories) Application pool's Advanced Settings: Enable 32-Bit Applications : True


EDIT: Another detail, tied to query string preservation:

Per suggestion in this post (http://www.developerfusion.com/code/4678/permanent-301-redirect-with-querystring-in-iis/)

Add $S$Q at the end of the domain and make sure the box for Redirect all requests to exact destination is checked. Then it will save the query string as well.

查看更多
登录 后发表回答