How to Set Up HTTP/HTTPS Redirection in IIS
Want to learn more about how to set up HTTP and HTTPS? Check out this tutorial on setting up HTTP and HTTPS redirection in IIS.
Join the DZone community and get the full member experience.
Join For FreeBefore you can set out with an HTTP/HTTPS redirect in IIS, you’ll need to make sure that you have an SSL certificate already installed. This is not a step you can skip. Once you’ve got the certificate installed, we can start working on a URL rewrite to redirect your traffic to your new-fangled HTTPS site.
Let’s Get Started
1. Download and install the IIS URL Rewrite Module
2. Open IIS Manager and, in the console, select the website you want to redirect
3. Select URL Rewrite
4. Click Add Rules
5. Select Blank Rule, click OK
6. Enter the Name of rule
7. In the Match URL section, choose “Matches the Pattern” in the Requested URL drop-down
8. Next, select “Regular Expressions” in the Using drop-down
9. In the Match URL section, enter: “(.*)”
10. In the conditions section, select Match All under Logical Grouping, then click Add
11. In the next window:
- Enter {HTTPS} as the condition input
- Select “Matches and Pattern” from the drop-down
- Enter ^OFF$ as the pattern
- Click OK
12. In the Action section, click Redirect and then specify the Redirect URL as https://{HTTP_HOST}/{R:1}
13. Check the Append Query String box
14. Choose your redirection type (301)
15. Click Apply
Apply the Rule to Your Website
1. In the IIS dashboard, right click on your site and select Explore
2. Your Root Directory will open — select the web.config file and open it
3. Make sure the file contains the following code block (if not, add it):
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name=”HTTPS force” enabled=”true” stopProcessing=”true”>
<match url=”(.*)” />
<conditions>
<add input=”{HTTPS}” pattern=”^OFF$” />
</conditions>
<action type=”Redirect” url=”https://{HTTP_HOST}/{R:1}” redirectType=”Permanent” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
4. If you have a web.config file in your root directory, create a new .txt file and place the above code in it, save and then rename the file to web.config.
Hope this helps!
Published at DZone with permission of S.Prakash Chowdhry. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments