IIS 10 redirect http to https://mysite.com

Fiorano2022 1 Reputation point
2022-03-07T14:44:22.483+00:00

Hi,

Ive set up IIS and successfully added an SSL so I can browse to https://mysite.com . Im trying to work out if a user enters http://mysite.com or mysite.com how to redirect them to https.. is this easily achievable?

These are my 'bindings':
180640-screenshot-2022-03-07-at-143737.png

Ive tried HTTP Redirect (below) but that doesnt seem to work (i get a HTTP Error 403):
180724-screenshot-2022-03-07-at-143913.png

If I do apply the HTTP redirect - it knocks out the HTTPS...

Really appreciate any advice.

Many thanks,
Fiorano

Internet Information Services
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Jose Zero 576 Reputation points
    2022-03-08T00:11:12.437+00:00

    You need a URL Rewrite Rule, check this link redirect-from-http-to-https-using-the-iis-url-rewrite-module

    0 comments No comments

  2. Bruce Zhang-MSFT 3,736 Reputation points
    2022-03-08T03:02:11.52+00:00

    Hi @Fiorano2022 ,

    In IIS server, if you want to redirect http request to corresponding https, you can try to use url rewrite module. Refer to the following steps:

    1. If this module is not already installed, navigate to https://www.iis.net/downloads/microsoft/url-rewrite and install the Url rewrite module.
    2. In IIS server, select the website you want to apply redirection to, then right click and select option explorer.
    3. Find the file web.config, then open it as txt file. If not exist, just create it.
    4. Add these configuration below into web.config : <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <directoryBrowse enabled="false" /> <rewrite> <rules> <rule name="HTTPS Redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" /> </rule> </rules> </rewrite> </system.webServer> </configuration>

    If you already have other rewrite rules, just copy the <rule> section into the configuration file.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards, Bruce Zhang

    0 comments No comments

  3. Fiorano2022 1 Reputation point
    2022-03-17T14:20:48.78+00:00

    HI Guys,

    Firstly, thanks for your replies!

    I think Im getting there :-) . At the moment:

    http://www.mydomain.co.uk redirects to https://www.mydomain.co.uk
    www.mydomain.co.uk redirects to https://www.mydomain.co.uk
    http://mydomain.co.uk redirects to https://www.mydomain.co.uk

    The only think that isnt working in https://mydomain.co.uk redirecting to https://www.mydomain.co.uk. If I type this in I get a HTTP Error 404. The requested resource is not found.

    I have bindings of :
    184146-screenshot-2022-03-17-at-141713.png

    And the following URL Rewrite Rules:

    <rule name="www fixer" enabled="true" stopProcessing="true">  
                <match url=".*$" />  
                <conditions logicalGrouping="MatchAll">  
                <add input="{HTTP_HOST}" pattern="www" negate="true" />  
                </conditions>  
                <action type="Redirect" url="https://www.mydomain.co.uk{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />  
        </rule>    
      
      
        <rule name="HTTP TO HTTPS" stopProcessing="true">  
            <match url="(.*)" />  
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">  
                <add input="{HTTPS}" pattern="^OFF$" />  
            </conditions>  
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />  
        </rule>  
    

    To finally get https:// redirecting to https://www is the final missing piece.

    Id really appreciate any advice on how to resolve this.

    Many thanks

    0 comments No comments

  4. Bruce Zhang-MSFT 3,736 Reputation points
    2022-03-18T06:06:10.467+00:00

    Hi @Fiorano2022 ,

    http://www.mydomain.co.uk redirects to https://www.mydomain.co.uk    
    www.mydomain.co.uk redirects to https://www.mydomain.co.uk    
    http://mydomain.co.uk redirects to https://www.mydomain.co.uk    
    

    They match to rule HTTP TO HTTPS so they can work well.

    But the url https://mydomain.co.uk. First it is a https request, so it doesn't match rule HTTP TO HTTPS, second is your site didnot bind to host name mydomain.co.uk. That means the request didnot send to IIS. So you need to add a binding with host name mydomain.co.uk.

    There's an error in the rule www fixer. Server variable {HTTP_HOST} means host name, not www prefix.
    184337-1.png

    If you want to add www, please use this rule:

    <rule name="Add WWW" stopProcessing="true">  
                  <match url="^(.*)$" />  
                  <conditions>  
                     <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />  
                  </conditions>  
                  <action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Bruce Zhang

    0 comments No comments

  5. Fiorano2022 1 Reputation point
    2022-03-28T15:47:26.44+00:00

    Hi @Bruce Zhang-MSFT ,

    Thanks once again for your reply.

    So, are you saying that I need to change the binding for the site from www.mysite.co.uk to mysite.co.uk ? and then add the following 2 rules :

    <rule name="HTTPS Redirect" stopProcessing="true">  
                         <match url="(.*)" />  
                         <conditions>  
                             <add input="{HTTPS}" pattern="^OFF$" />  
                         </conditions>  
                         <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />  
                     </rule>  
    
    
    <rule name="Add WWW" stopProcessing="true">  
                   <match url="^(.*)$" />  
                   <conditions>  
                      <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />  
                   </conditions>  
                   <action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />  
    

    Many thanks,

    Fiorano