Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS HTTP to HTTPS relative redirect

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.

like image 449
user2234612 Avatar asked Apr 02 '13 05:04

user2234612


People also ask

How do I create an HTTP redirect in IIS?

In the Web Server (IIS) pane, scroll to the Role Services section, and then click Add Role Services. On the Select Role Services page of the Add Role Services Wizard, expand Common Http Features, select HTTP Redirection, and then click Next. On the Confirm Installation Selections page, click Install.


2 Answers

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.

like image 198
AndyH Avatar answered Sep 22 '22 12:09

AndyH


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>
like image 40
Hubo Avatar answered Sep 21 '22 12:09

Hubo