Redirect HTTP to HTTPS via IIS
Prologue
In this article, I’ll talk about how to redirect HTTP
to HTTPS
in IIS. And, a briefing on how to import SSL certificate. 😏
1. Enable HTTPS
Before we redirect HTTP
to HTTPS
, we have to acquire a SSL certificate. You can get it from your server provider, or elsewhere. Once you have it, upload it to your server first.
1.1 Import SSL Certificate
Then, open your IIS homepage, and find “Server Certificate” under IIS section.
Double-click to open it, and select “Import” to import existing certificate you just uploaded.
Then, just find your certificate with suffix .pfx
. Usually, there will be a plain text file accompanied, which contains the password for this certificate. Then, just click “OK”, and the certificate will then be imported.
1.2 Enable HTTPS
for Website
Now, we can enable HTTPS
for our website.
Usually, free certificate may only apply to host name of www
and port 443.
Open the website you want to use HTTPS
, and click “Bind”.
Then, in the popup dialog, choose “Add”, and select type of “https” with the certificate you just added. After these, just click “OK”.
Now, you can visit this site using https://
or port 443.
2. Configure Auto Redirect
After HTTPS
enabled, you may found that it could not automatically redirect HTTP
request. So, we have to configure it a step further.
2.1 URL Rewrite
To achieve this, we have to download IIS URL Rewrite tool. You can find download links here.
Just scroll down to the bottom of the page, and you’ll find plenty of installers. After installation, you will see “URL Rewrite” option in homepage.
2.2 Add Rewrite Rule
Reference: Redirect HTTP to HTTPS via IIS
Open the homepage of your website, and then open “URL Rewrite” and click “Add Rule”, then “Blank Rule”.
2.2.1 Matching URL
In the first section, we choose “Matches the pattern”, and using “Wildcards”. And set pattern to *
.
2.2.2 Condition
In condition, we add an HTTPS
condition. I don’t know why, but the pattern here should not be *
.
I really don’t know why the pattern here should not be *
. I tried to use that at first, but it won’t work, and even make both http://
and https://
unavailable. 🤒
2.2.3 Action
At last, in action section, set “Redirect” as action type, and set redirect URL to https://{HTTP_HOST}{REQUEST_URI}
. Check “Append Query String”, and select a redirection type.
301 is a better redirection type when you choose to redirect a http
site to a https
one. For more information on redirection type, you can refer to this: What Is a 301 or 302 Redirect?
At last, your rewrite rule should look like this. And your website will be automatically redirected to https://
.🥳
Epilogue
Tada! Enjoy your HTTPS site! 🤪