We have so many clients asking about this issue. So, we decide to write this tutorial and hope this information can help other people too. In this review, we will write simple tutorial about how to setup http/https redirection in IIS.
There are lots of routing options accessible in ASP.NET but still it comes a time when you need to manipulate a URL and manipulating it outside a code comes handy. When this happens, the best you can do id to use IIS Rewrite Module. Transforming various URL’s out of code enables you to do various things including performing redirections for archive or transferred content without interfering with the code, you can easily implement SEO optimizations and tweaks quickly and easily without code and many more. Below is a collection of useful IIS rewrite rules that will help you understand IIS rewrites.
Useful IIS Rewrite Rules
Adding www Prefix
This is a basic rule that adds prefix “www” to any URL you need. This is a requirement for SEO.
Redirection from Domain 1 to Domain 2
This rule comes handy when you change the name of your site or may be when you need to catch and alias and direct it to your main site. If the new and the old URLs share some elements, then you could just use this rule to have the matching pattern together with the redirect target being.
HTTPS/HTTP Redirection
Redirecting users from HTTP to HTTPS is one of the reasons that you need to apply useful IIS rewrite rules. It can lead to conditional statements while looking for dev/test mode in your code. This rules allows you to handle the redirection without much statements which is tidier.
There is a pair of rules in this case each for one of the two ways. In both the rules, a check is performed to verify that the protocol used is http/https. The rules work on the same URL patterns or the similar lists of pages to match. For the redirect to HTTP, it is not about matching the pages; it is a reverse of the first rule and usually have a number of .NET/site paths that are excluded.
Setup Redirection Using IIS
Above steps is to setup URL redirection via your code. But, if you manage your own server, you can also setup redirection via IIS. The following is the steps
1. Download and install the “URL Rewrite” module.
2. Open the “IIS Manager” console and select the website you would like to apply the redirection to in the left-side menu:
3. Double-click on the “URL Rewrite” icon.
4. Click “Add Rule(s)” in the right-side menu.
5. Select “Blank Rule” in the “Inbound” section, then press “OK”:
6. Enter any rule name you wish.
7. In the “Match URL” section:
- Select “Matches the Pattern” in the “Requested URL” drop-down menu
- Select “Regular Expressions” in the “Using” drop-down menu
- Enter the following pattern in the “Match URL” section: “(.*)”
- Check the “Ignore case” box
8. In the “Conditions” section, select “Match all” under the “Logical Grouping” drop-down menu and press “Add”.
9. In the prompted window:
- Enter “{HTTPS}” as a condition input
- Select “Matches the Pattern” from the drop-down menu
- Enter “^OFF$” as a pattern
- Press “OK”
10. In the “Action” section, select “Redirect” as the action type and specify the following for “Redirect URL”:
https://{HTTP_HOST}/{R:1}
11. Check the “Append query string” box.
12.Select the Redirection Type of your choice. The whole “Action” section should look like this:
NOTE: There are 4 redirect types of the redirect rule that can be selected in that menu:
- Permanent (301) – preferable type in this case, which tells clients that the content of the site is permanently moved to the HTTPS version. Good for SEO, as it brings all the traffic to your HTTPS website making a positive effect on its ranking in search engines.
- Found (302) – should be used only if you moved the content of certain pages to a new place *temporarily*. This way the SEO traffic goes in favour of the previous content’s location. This option is generally not recommended for a HTTP/HTTPS redirect.
- See Other (303) – specific redirect type for GET requests. Not recommended for HTTP/HTTPS.
- Temporary (307) – HTTP/1.1 successor of 302 redirect type. Not recommended for HTTP/HTTPS.
13. Click on “Apply” on the right side of the “Actions” menu.
The redirect can be checked by accessing your site via http:// specified in the URL. To make sure that your browser displays not the cached version of your site, you can use anonymous mode of the browser.
The rule is created in IIS, but the site is still not redirected to https://
Normally, the redirection rule gets written into the web.config file located in the document root directory of your website. If the redirection does not work for some reason, make sure that web.config exists and check if it contains the appropriate rule.
To do this, follow these steps:
1. In the sites list of IIS, right-click on your site. Choose the “Explore” option:
2. “Explore” will open the document root directory of the site. Check if the web.config file is there.
3. The web.config file must have the following code block:
<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 the web.config file is missing, you can create a new .txt file, put the aforementioned code there, save and then rename the file to web.config.