You need a URL Rewrite Rule, check this link redirect-from-http-to-https-using-the-iis-url-rewrite-module
IIS 10 redirect http to https://mysite.com
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':
Ive tried HTTP Redirect (below) but that doesnt seem to work (i get a HTTP Error 403):
If I do apply the HTTP redirect - it knocks out the HTTPS...
Really appreciate any advice.
Many thanks,
Fiorano
7 answers
Sort by: Most helpful
-
-
Bruce Zhang-MSFT 3,751 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:
- If this module is not already installed, navigate to https://www.iis.net/downloads/microsoft/url-rewrite and install the Url rewrite module.
- In IIS server, select the website you want to apply redirection to, then right click and select option
explorer
. - Find the file
web.config
, then open it as txt file. If not exist, just create it. - 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
-
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.ukThe 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 :
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
-
Bruce Zhang-MSFT 3,751 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 ruleHTTP TO HTTPS
, second is your site didnot bind to host namemydomain.co.uk
. That means the request didnot send to IIS. So you need to add a binding with host namemydomain.co.uk
.There's an error in the rule
www fixer
. Server variable{HTTP_HOST}
means host name, not www prefix.
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 -
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