• Sales: 1-800-961-2888
  • Support: 1-800-961-4454

How do I rewrite URLs from ASP/.NET on Cloud Sites?


Contents

The IIS 7 URL Rewrite Module

All new .NET sites created on The Rackspace Cloud will have the URL Rewrite Module available, which allows rewrite rules to be defined in the Web.config.

Example #1

You can utilize the URL Rewrite Module to rewrite ASP, ASPX, or even HTML pages. In this 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

The URL Rewrite Module also supports regular expressions. You can match characters in the incoming URL and then use {R:#} ({R:1}, {R:2}, etc.) to reference them in the resulting URL. In this example, we want to match 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

The URL Rewrite Module also provides a method to create a catch-all rewrite rule. This is similar to how applications like Wordpress use Apache's mod_rewrite to create pretty permalinks or 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 to default.aspx:

 
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
     <rules>
      <rule name="Rewrite All" stopProcessing="true">
            <match url="^(.*)$" />
            <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

The following example shows an iis rewrite reading the host header to do a 301 redirect. The example below takes a non www domain and redirects 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.com$" />
          </conditions>
          <action type="Redirect" url="http://www.mydomain.com/{R:0}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

References

The following links are from iis.net, The Official Microsoft IIS Site:

Third-party Solutions

Prior to the URL Rewrite Module, web developers had to create this functionality via application code or by generating their own assemblies to implement this feature. Fortunately for application developers, many parties realized this was such a necessary feature; so rather than have every developer reinvent the wheel, these parties created modules that could be deployed to add this functionality with very little effort on the part of the web developer. Some of these third-party modules are:

For older sites on the Cloud Sites platform, we recommend deploying an HttpModule to handle URL rewrites; in this article we will explore UrlRewriter.NET.

Getting Started

To get started, please follow this article on bin deploying UrlRewriter.NET.

Using UrlRewriter.NET

UrlRewriter.NET provides a full reference guide as well as examples of common rules. When bin deploying UrlRewriter.NET, you should have added a configSections element that causes the rewriter configuration section to be processed by UrlRewriter.NET. The following are examples of rewrite rules using this configuration element.

Example #1

In this example, we match info and display the contents of info.aspx:

 
<rewriter>
  <rewrite url="~/info" to="~/info.aspx" />
</rewriter>

Example #2

UrlRewriter.NET also supports regular expressions. You can match characters in the incoming URL and then use $# ($1, $2, etc.) to reference them in the resulting URL. In this example, we want to match products/id and have it processed by products.aspx as a standard GET request:

 
<rewriter>
  <rewrite url="~/products/(.+)" to="~/products.aspx?category=$1" />
</rewriter>

References







© 2011-2013 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER