I am one of these older web developers still insisting on hosting on my own server and therefore also managing IIS myself. This Topiq is a documentation of the most common IIS redirects I need to do.
Index :
Appendixes :
Note that in this tutorial I use the IIS Redirect UI to create the redirect rules, however the Redirect UI will save the redirect rules to web.config and then IIS is serving a website, it will apply the redirect rules as found in web.config - you can therefore also just write (or copy) any redirect rule directly into the web.config.
Redirect http to https
Steps to setup http to https redirection in IIS:
- Open IIS and select the website for which you want to redirect http -> https and then double click on the "URL Rewrite" icon.
- Click the "Add Rule(s).." link in the right pane.
- Under "Inbound rules" select the "Blank rule" option and click the "OK" button.
- On the "Edit Inbound Rule" page :
- Name: http to https
- Match URL
- Requested URL: Matches the Pattern
- Using: Regular Expressions
- Pattern: (.*) : capture the full path.
- Ignore case: true
- Conditions
- Logical grouping: Match All
- Add condition
- Click the "Add" button.
- Fill in the "Add Condition" dialog
- Condition input: {HTTPS} : returns either ON or OFF whether the request came through a secure channel.
- Check if input string: Matches the Pattern
- Pattern: ^OFF$ : only activate this rule if the request come through an unsecure channel.
- Ignore case: true
- Press the "OK" button to save the condition.
- Redirect match condition is added.
- Track capture groups across conditions: false
- Server Variables : No server variables will be used.
- Action
- Action Type: Redirect
- Action Properties
- Redirect URL: https://{HTTP_HOST}/{R:0} : redirect to the requested domain {HTTP_HOST} appending the requested path {R:0} captured in the Match URL Pattern.
- Append query string: true
- Redirect type: Permanent (301)
- Press the "Apply" button in the right Actions pane.
- http to https redirect rule successfully created.
How the http to https redirect rule is looking in web.config:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Note that alternatively to the http to https redirection rule, you can in ASP.NET Core also setup redirection VERY EASILY using the redirection middleware in the request pipeline like this :
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
//... other pipeline configuration
}
Also note that UseHttpsRedirection() in ASP.NET Core is unnessary if served behind a reverse proxy (here IIS) already implementing http to https.
Redirect subdomain to main domain
Here from www.topiqs.online to topiqs.online.
Steps to redirect subdomain to main domain:
- Open IIS and select the website for which you want to redirect http -> https and then double click on the "URL Rewrite" icon.
- Click the "Add Rule(s).." link in the right pane.
- Under "Inbound rules" select the "Blank rule" option and click the "OK" button.
- On the "Edit Inbound Rule" page :
- Name: subdomain to main domain
- Match URL
- Requested URL: Matches the Pattern
- Using: Regular Expressions
- Pattern: (.*) : capture the full path.
- Ignore case: true
- Conditions
- Logical grouping: Match All
- Add condition
- Click the "Add" button.
- Fill in the "Add Condition" dialog
- Condition input: {HTTP_HOST} : get the requested domain to test the pattern against.
- Check if input string: Matches the Pattern
- Pattern: www.topiqs.online : only activate the redirect rule if the requested domain is www.topiqs.online.
(Note that if you have more than one subdomain binding that you want to redirect, you can use a more general pattern like eg. .+\.topiqs.online - here catching ALL subdomains) - Ignore case: true
- Press the "OK" button to save the condition.
- Redirect match condition is added.
- Track capture groups across conditions: false
- Server Variables : No server variables will be used.
- Action
- Action Type: Redirect
- Action Properties
- Redirect URL: https://topiqs.com/{R:0} : redirect to top domain and append the request Path.
- Append query string: true
- Redirect type: Permanent (301)
- Press the "Apply" button in the right Actions pane.
- http to https redirect rule successfully created.
How the IIS subdomain redirect rule looks in web.config :
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="subdomain to main domain" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.topiqs.online" />
</conditions>
<action type="Redirect" url="https://topiqs.online/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Redirect old-domain to new-domain
I had this problem when I wanted to change a domain name from manylink.online to topiqs.online. Redirecting the domain ensures that :
- Users finding content via Google on the manylink.online domain will be redirected to the same content on topiqs.online.:
- I think (and am nearly sure) that canonical urls are unnecessary on existing content on old-domain (manylink.online).
There are 2 methods to create the old-domain to new-domain redirection:
- Add a URL Redirect rule in IIS : the method I now use.
- Add a URL Redirect DNS record : easiest method but problematic.
Steps to redirect old-domain to new-domain in IIS:
- Open IIS and select the old-domain website for which you want to redirect from and then double click on the "URL Rewrite" icon.
- Click the "Add Rule(s).." link in the right pane.
- Under "Inbound rules" select the "Blank rule" option and click the "OK" button.
- On the "Edit Inbound Rule" page :
- Name: redirect to topiqs.online
- Match URL
- Requested URL: Matches the Pattern
- Using: Regular Expressions
- Pattern: (.*) : capture the full path.
- Ignore case: true
- Conditions
- Logical grouping: Match All
- Add condition
- Click the "Add" button.
- Fill in the "Add Condition" dialog
- Condition input: {HTTP_HOST} : get the requested domain to test the pattern against.
- Check if input string: Matches the Pattern
- Pattern: manylink.online : only activate the rule if the domain contains "manylink.online".
- Ignore case: true
- Press the "OK" button to save the condition.
- Redirect match condition is added.
- Track capture groups across conditions: false
- Server Variables : no server variables will be used.
- Action
- Action type: Redirect
- Action Properties
- Redirect URL: https://topiqs.online/{R:0} : redirect to topiqs.online and append the request Path.
- Append query string: true
- Redirect type: Permanent (301)
- Press the "Apply" button in the right Actions pane.
- old-domain to new-domain redirect rule successfully created.
How the IIS domain redirect rule looks in web.config :
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="redirect to topiqs.online" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="manylink.online" />
</conditions>
<action type="Redirect" url="https://topiqs.online/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
URL Redirect DNS records
If you have edit access to your domains DNS records, redirect an old domain to a new domain is most easily done by a URL Redirect DNS record.
There are 3 types of URL Redirect DNS records:
- 301 Unmasked Permanent Redirect : new-domain is shown in browsers url field and new-domain is indexed by search engines.
- 302 Unmasked Redirect : new-domain is shown in browsers url field but old-domain is indexed by search engines.
- Masked Redirect : old-domain is shown in browsers url field. I don't know exactly how search engines is handling this, my guess is it is not good.
However, there are 2 reasons I do NOT any longer use URL Redirect DNS records :
- Most importantly I had the problem that a DNS Redirect DNS record messed up browsers connection process sometimes taking more than 20 seconds to just connect - it tooks me so loooong time to figure it out that I am too ashamed of talking about it in public.
- Another problem I experienced then applying a URL Redirect DNS records was that requesting a LetsEncrypt certificate fails with the following error "Authorization result: invalid" & "Timeout during connect (likely firewall problem)" (see How to install LetsEncrypt SSL/TLS certificate on IIS for more info on that error).
Appendix : Understanding IIS Redirection Rules
The main flow of an IIS Redirect rule typically consist of these 3 main steps :
- Match the request path for situations where we want to redirect and also capture parts of the url for back references - denoted R:X, eg. the full match is R:0.
- Setup further conditions for whether we want to redirect and also capture parts of the conditions for back references - denoted C:X, eg. the full match is C:0.
- Create the redirect rule possibly using any back references R:X & C:X.
Then using the IIS Rewrite/Redirection UI to build the redirect rule (rather than writing to the web.config directly), there are 3 fields matching the above 3 main steps :
- Match URL > Pattern : regards the request Path (not the protocol, not the domain, not the query string). In the Redirect URL field (below) we can refer to the whole path as {R:0} and if we use capture groups, eg. /home/(category), we can refer to them using the group number, eg. here {R:1} would be "category". In all the examples above I just use (.*), which means that {R:0} & {R:1} hold the same value.
- Conditions :
- Condition input : the input seems to be in terms of server variables.
- {QUERY_STRING} : returns the request querystring.
- {HTTP_HOST} : I am unsure what this variable returns precisely, however assuming it returns the request hostname have so far worked for me.
- {HTTPS} : returns ON if the request came through a secure channel (eg. SSL), otherwise it returns OFF.
- Action > Action Properties > Redirect URL : if the redirect rule is activated (based on the Match URL > Pattern and any Conditions), then send send the Redirect URL back to the browser (together with the redirect http code).