URL Rewrite is an add-on for the IIS (Internet Information Services) web server that allows to create redirect rules and process URLs. With URL Rewrite, you can perform tasks such as redirects from HTTP to HTTPS or from www to non-www URLs, and implement canonical URLs and URLs that are easy for users to remember and optimized for search engines. In this article, we’ll show you how to install and configure IIS URL rewrite rules.
Prerequisites:
- Computer running a supported version of Windows (all Windows Servers 2022,2019,2016,2012R2 and Windows 11/10/8 desktop editions are supported.1);
- Internet Information Services 8.0 (or newer) role installed (currently the latest version of IIS is IIS10);
- Web site that is running on IIS.
How to Configure Redirect Rules with IIS Rewrite Module?
Download the Rewrite Module 2.1 installation package (https://www.iis.net/downloads/microsoft/url-rewrite). There are x86/x64 versions of URL Rewrite, as well as separate language versions.
In our example, we will download the x64 installer for Windows Server 2019.
Run the rewrite_amd64_en-US.msi file and install the URL Rewrite Module.
Now open the Internet Information Services (IIS) Manager console from Administrative Tools in the Control Panel (or by using the inetmgr command).
Select your site and check if a new URL Rewrite section has appeared in the central pane.
Let’s consider a simple URL rewrite rule that redirects a URL from WWW to non-WWW.
Right-click on the site and select Edit Bindings.
Check if you have added site addresses with and without www (for example, www.webportal.contoso.com and webportal.contoso.com).
Run your browser and check if your website opens at both URLs (that’s not good from an SEO perspective!).
Now we are going to use the URL Rewrite Rule to enable the redirection from the www to the non-www URL.
Select the URL Rewrite module and click Add rule in the right pane.
You can create a rewrite rule manually, or by using one of the templates. In this example, we will use the predefined Canonical domain name template from the Search Engine Optimization section.
Specify the primary host name (without www):
webportal.contoso.com
The new rule will appear in the URL Rewrite list. This rule is enabled by default.
Verify that IIS now automatically redirects you to webportal.contoso.com when you open www.webportal.contoso.com in a browser.
You can open the properties of this rule in the IIS Manager.
In our case, the following redirection rule was automatically generated:
- Request URL: Matches the Pattern
- Using: Regulat Expression
- Pattern: (.*)
- Ignore case: True
- Logical Grouping: Match all
- Condition input: {HTTP_HOST}
- Does Not Match the Pattern
- Pattern: ^webportal\.contoso\.com$
- Action: redirect
- Redirect URL: http://webportal.contoso.com/{R:1}
- Redirect type: Permanent (301)
The URL Rewrite module stores its rules in the web.config site configuration file (XML format). The following section has been added to the file in our example:
<rewrite> <rules> <rule name="CanonicalHostNameRule1"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^webportal\.contoso\.com$" negate="true" /> </conditions> <action type="Redirect" url="http://webportal.contoso.com/{R:1}" /> </rule> </rules> </rewrite>
Hint. The web.config file stores the settings for the website. The applicationHost.config file will be used if you need to create a global URL rewrite rule for all IIS sites on the Windows Server.
Another popular URL rewrite rule is the redirection from HTTP to a secure HTTPS site address.
Before you create it, you will need to create a Certificate Signing Request (CSR) for the IIS, get an SSL Certificate, and bind it to the IIS site. Then add the following URL Rewrite rule to your web.config file:
<rewrite> <rules> <clear /> <rule name="RedirectHTTPtoHTTPS" enabled="true"> <match url="(.*)" ignoreCase="false" /> <conditions> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" /> </rule> </rules> </rewrite>
You can create your own redirect rules in IIS using the URL Rewrite.