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条回答
We Are One
2楼-- · 2020-03-01 07:48

I found a way to do this, and you don't need the Rewrite module for it.
The following worked for me on Windows 8 (IIS 8.5):

  1. Remove the HTTP binding from your site (leave HTTPS in place)
  2. Add another site
  3. Make sure that the new site has HTTP binding
  4. Configure HTTP Redirect as shown:

enter image description here

Now all HTTP request will redirect to your HTTPS site and will preserve the rest of the URL.

查看更多
The star\"
3楼-- · 2020-03-01 07:48

I had the same problem where the R:1 was dropping my folders. I fixed it like this.

<rule name="http to https" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
       <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
            appendQueryString="false" redirectType="SeeOther" />
</rule>
查看更多
神经病院院长
4楼-- · 2020-03-01 07:50

I believe AndyH's answer to be the easiest and best way. I have found using the URL rewrite can also conflict with code that may redirect the user to another page. IT commonly broke in our environment. But Andy's solution worked flawlessly. I also think Andy's solution will put less overhead on the server as it doesn't need to examine every url hitting it for possible re-write conditions.

查看更多
甜甜的少女心
5楼-- · 2020-03-01 07:52


I found a workaround:

Consider what in IIS is consired a website: simply a set of rules, the path in which get files and its bindings.

Furthermore, there's available a function called "HTTP Redirect" (included standardly in IIS), that redirect an host to another, keeping all subdirectory (it makes a relative path). The workaround is to leave just the binding for HTTPS (port 443) in your website, and create another with the binding on HTTP (port 80) and set for this an HTTP redirect to your URL with https://.

For example, consider a website called mytest and its urls http://www.mytest.com/ and https://www.mytest.com/. Set for it instead only binding on https://www.mytest.com/, and delete the http binding. Then create a new website with the same local path, called mytest http with just a binding over port 80 (http://www.mytest.com/) and set for this one an HTTP Redirect to https://www.mytest.com/.
Simple and clean, and that should be as fast as directly the https url for the user, because it's just an internal redirect. I hope that can work for you!

查看更多
手持菜刀,她持情操
6楼-- · 2020-03-01 07:53

I have found that the

<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />

syntax will only work for the website's ROOT web.config file.

If the rewrite rule is applied to a virtual web.config file, then use..

<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{URL}" />

The {URL} syntax will include the initial forward slash, the virtual path, and any URL parameters.

查看更多
聊天终结者
7楼-- · 2020-03-01 07:58

Change it to:

<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}" />
      </rule>
   </rules>
</rewrite>
查看更多
登录 后发表回答