ASP.NET Tips :: Few Examples URL Rewrite Module in ASP.NET – Windows ASP.NET Core Hosting 2024 | Review and Comparison

This post describes some of the tips and tricks that one may find useful when solving URL-based problems for their web server or web site.

Here are few examples how to use URL Rewrite Module in IIS 7

url-rewrite

Example 1

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

I hope this tutorial helpful for you. Happy coding!

[crp]

Leave a Reply