Thursday, March 03, 2011

IIS URL Rewrite Module

I have been setting up rewrite rules for a website I work on and thought this might be useful. To do so, I am using the URL Rewrite Module that is part of IIS. There is a UI for it in the IIS Manager URL Rewrite, but I have found that working directly in the web.config is easier. YMMV.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- Convert links to lower case for seo, but ignore system files, images, and code -->
        <rule name="Convert to lower case">
   <match url=".*[A-Z].*" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" pattern="^.*\.(ashx|axd|css|gif|png|js)$" negate="true" ignoreCase="true" />
          </conditions>
  <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
        </rule>
        <!-- If we have a product page that is not friendly, convert to friendly and redirect -->
        <!-- From: shopping/pages/product.aspx?cat=shoppingcatalog&category=books?pid=a12345?pdisplayname=book-display-name -->
        <!-- To: shopping/product/a12345/book-display-name -->
        <rule name="Redirect Product Rule" stopProcessing="true">
          <match url="^shopping/pages/product\.aspx$" />
          <conditions trackAllCaptures="true">
            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
            <add input="{QUERY_STRING}" pattern="cat=([0-9a-zA-Z\-]+)" />
            <add input="{QUERY_STRING}" pattern="category=([0-9a-zA-Z\-]+)" />
            <add input="{QUERY_STRING}" pattern="pid=([0-9a-zA-Z\-]+)" />
            <add input="{QUERY_STRING}" pattern="pdisplayname=([0-9a-zA-Z\-]+)" />
            <add input="{REQUEST_FILENAME}" pattern="^.*\.(ashx|axd|css|gif|png|js)$" negate="true" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="shopping/product/{C:3}/{C:4}" appendQueryString="false" />
        </rule>
        <!-- Convert a friendly URL back to raw from  -->
        <!-- From: shopping/product/a12345/book-display-name -->
        <!-- To: shopping/pages/product.aspx?cat=shoppingcatalog&category=books&pid=a12345&pdisplayname=book-display-name -->
        <rule name="Rewrite Product Rule" stopProcessing="true">
          <match url="^shopping/product/([^/]+)/([^/]+)?$" />
          <conditions trackAllCaptures="true">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^.*\.(ashx|axd|css|gif|png|js)$" negate="true" ignoreCase="true" />
          </conditions>
          <action type="Rewrite" url="shopping/pages/product.aspx?cat=ShoppingCatalog&amp;pid={R:1}&amp;pdisplayname={R:2}" />
        </rule>
        <!-- Convert a friendly URL back to raw from  -->
        <!-- From: shopping/product/a12345 -->
        <!-- To: shopping/pages/product.aspx?cat=shoppingcatalog&category=books&pid=a12345 -->
        <rule name="Rewrite Product Rule 2" stopProcessing="true">
          <match url="^shopping/product/([^/]+)?$" />
          <conditions trackAllCaptures="true">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^.*\.(ashx|axd|css|gif|png|js)$" negate="true" ignoreCase="true" />
          </conditions>
          <action type="Rewrite" url="shopping/pages/product.aspx?cat=ShoppingCatalog&amp;pid={R:1}" />
        </rule>
        <!-- If we have a category page that is not friendly, convert to friendly and redirect -->
        <!-- From: shopping/pages/category.aspx?cat=shoppingcatalog&category=books -->
        <!-- To: shopping/category/books -->
        <rule name="Redirect Category Rule" stopProcessing="true">
          <match url="^shopping/pages/category\.aspx$" />
          <conditions trackAllCaptures="true">
            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
            <add input="{QUERY_STRING}" pattern="cat=([0-9a-zA-Z\-]+)" />
            <add input="{QUERY_STRING}" pattern="category=([0-9a-zA-Z\-]+)" />
            <add input="{REQUEST_FILENAME}" pattern="^.*\.(ashx|axd|css|gif|png|js)$" negate="true" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="shopping/category/{C:2}" appendQueryString="true" />
        </rule>
        <!-- Convert a friendly URL back to raw from  -->
        <!-- To: shopping/category/books -->
        <!-- From: shopping/pages/category.aspx?cat=shoppingcatalog&category=books -->
        <rule name="Rewrite Category Rule 1" stopProcessing="true">
          <match url="^shopping/category/([^/]+)?$" />
          <conditions trackAllCaptures="true">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^.*\.(ashx|axd|css|gif|png|js)$" negate="true" ignoreCase="true" />
          </conditions>
          <action type="Rewrite" url="shopping/pages/category.aspx?cat=ShoppingCatalog&amp;category={R:1}" />
        </rule>
      </rules>
      <!-- outbound rules process A tags in pages being sent to users -->
      <outboundRules rewriteBeforeCache="true">
        <!-- convert all links to lowercase -->
        <rule name="Outbound lowercase" preCondition="IsHTML" enabled="true">
          <match filterByTags="A" pattern=".*[A-Z].*" ignoreCase="false" />
          <action type="Rewrite" value="{ToLower:{R:0}}" />
        </rule>
        <!-- From: shopping/pages/category.aspx?cat=shoppingcatalog&category=books&page=2 -->
        <!-- To: shopping/category/books?page=2 -->
        <rule name="Rewrite Category Outbound 1" preCondition="IsHTML" enabled="true" stopProcessing="true">
          <match filterByTags="A" pattern="category\.aspx\?cat=shoppingcatalog&amp;amp;category=([0-9a-zA-Z\-]+)&amp;amp;page=([0-9a-zA-Z\-]+)" ignoreCase="true" />
          <action type="Rewrite" value="/shopping/category/{R:1}?page={R:2}" />
        </rule>
        <!-- From: shopping/pages/category.aspx?cat=shoppingcatalog&category=books -->
        <!-- To: shopping/category/books -->
        <rule name="Rewrite Category Outbound 2" preCondition="IsHTML" enabled="true" stopProcessing="true">
          <match filterByTags="A" pattern="category\.aspx\?cat=shoppingcatalog&amp;amp;category=([0-9a-zA-Z\-]+)" ignoreCase="true" />
          <action type="Rewrite" value="/shopping/category/{R:1}" />
        </rule>
        <!-- From: shopping/pages/product.aspx?cat=shoppingcatalog&category=books&pid=a12345&pdisplayname=book-display-name -->
        <!-- To: shopping/product/a12345/book-display-name -->
        <rule name="Rewrite Product Outbound 1" preCondition="IsHTML" enabled="true" stopProcessing="true">
          <match filterByTags="A" pattern="product\.aspx\?cat=shoppingcatalog&amp;amp;category=([0-9a-zA-Z\-]+)&amp;amp;pid=([0-9a-zA-Z\-]+)&amp;amp;pdisplayname=([0-9a-zA-Z\-]+)" ignoreCase="true" />
          <action type="Rewrite" value="/shopping/product/{R:2}/{R:3}" />
        </rule>
        <!-- From: shopping/pages/product.aspx?cat=shoppingcatalog&category=books&pid=a12345 -->
        <!-- To: shopping/product/a12345 -->
        <rule name="Rewrite Product Outbound 2" preCondition="IsHTML" enabled="true" stopProcessing="true">
          <match filterByTags="A" pattern="product\.aspx\?category=([0-9a-zA-Z\-]+)&amp;amp;cat=shoppingcatalog&amp;amp;pid=([0-9a-zA-Z\-]+)" ignoreCase="true" />
          <action type="Rewrite" value="/shopping/product/{R:2}" />
        </rule>
        <!-- From: shopping/pages/product.aspx?pid=a12345&pdisplayname=book-display-name -->
        <!-- To: shopping/product/a12345/book-display-name -->
        <rule name="Rewrite Product Outbound 3" preCondition="IsHTML" enabled="true" stopProcessing="true">
          <match filterByTags="A" pattern="product\.aspx\?pid=([0-9a-zA-Z\-]+);amp;pdisplayname=([0-9a-zA-Z\-]+)" ignoreCase="true" />
          <action type="Rewrite" value="/shopping/product/{R:1}/{R:2}" />
        </rule>
        <!-- From: shopping/pages/product.aspx?cat=shoppingcatalog&pid=a12345&pdisplayname=book-display-name -->
        <!-- To: shopping/product/a12345/book-display-name -->
        <rule name="Rewrite Product Outbound 4" preCondition="IsHTML" enabled="true" stopProcessing="true">
          <match filterByTags="A" pattern="product\.aspx\?cat=shoppingcatalog&amp;amp;pid=([0-9a-zA-Z\-]+)&amp;amp;pdisplayname=([0-9a-zA-Z\-]+)" ignoreCase="true" />
          <action type="Rewrite" value="/shopping/product/{R:1}/{R:2}" />
        </rule>
        <preConditions>
          <!-- Only process html files -->
          <preCondition name="IsHTML" logicalGrouping="MatchAny">
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
          </preCondition>
        </preConditions>
      </outboundRules>
    </rewrite>
  </system.webServer>
<configuration>

No comments:

Post a Comment