IIS URL Rewrite Module in Cloud Sites
Note: |
| The code provided below is only an example. Your specific website may require different code. Unfortunately, as outlined in our Terms of Service and Cloud Sites Spheres of Support Expectations, we are unable to help you troubleshoot code issues. We recommend speaking with your developer before implementing any scripts. |
The URL Rewrite Module is for sites using Windows as the technology for your site structure. Cloud Sites uses IIS 8.5 as the primary configuration and all new Cloud Sites using ASP.NET have the IIS URL Rewrite Module enabled automatically. This allows you to define URL rewrite rules in the web.config file.
In this tutorial, I will walk you through four common examples of URL rewrites and redirects using ASP.NET. For more information on using and deploying rewrite modules, please see the official documentation on the Microsoft IIS site:
Creating Rewrite Rules for the URL Rewrite Module
URL Rewrite Module Configuration Reference
Warning: |
| It is important to use caution when running unfamiliar scripts. If you are unfamiliar with rewriting URLs using ASP, please consult a developer to help you deploy them. The following URL rewrites are for example only and should not be used verbatim. |
Example 1: Rewrite Pages
You can use the URL Rewrite Module to rewrite ASP, ASPX and HTML pages. In the following example, we match example.asp, example.aspx and example.html and display the contents of rewrite.aspx.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Rewrite ASP"> <match url="example.asp" /> <action type="Rewrite" url="rewrite.aspx" /> </rule> <rule name="Rewrite ASPX"> <match url="example.aspx" /> <action type="Rewrite" url="rewrite.aspx" /> </rule> <rule name="Rewrite HTML"> <match url="example.html" /> <action type="Rewrite" url="rewrite.aspx" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
Example 2: Regular Expressions
The URL Rewrite Module also supports regular expressions. You can match characters in the incoming URL and then use {R:#} (example: {R:1} and {R:2}) to reference them in the resulting URL. In the following example, we want to match the article/id/*title and have it processed by article.aspx as a standard GET request.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Articles">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>Example 3: Catch-All Rewrite Rule
The URL Rewrite Module also provides a method to create a catch-all rewrite rule. This is similar to the way that applications like WordPress use the Apache mod_rewrite module to create pretty permalinks for friendly URLs by having a single page load content based on the incoming URL. In this example, we use the URL Rewrite Module and a simple regular expression to catch anything that isn’t a file or directory and send it all to default.aspx.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> ;<rule name="Rewrite All" stopProcessing="true"> <match url="^(.*)
quot; /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="article.aspx" /> </rule> </rules> </rewrite> </system.webServer> </configuration>Example 4: Create Redirects
The following example shows an IIS rewrite reading the host header to perform a 301 redirect. This example takes a non-www domain and redirects it to a www.mydomain.com domain.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="301 Redirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mydomain.comquot; /> </rule> </rules> </rewrite> </system.webServer> </configuration>