Friendly urls with iis, using url rewrites..


A ThatsIT Solutions Tutorial

Writing friendly urls is not a one step procedure, not only do you need to rewrite the new urls to the old ugly urls, you need to create corresponding 301 redirects to ensure that the old urls are not exposed and leading to duplicate content, you also need to either fix every internal link to point to the new friendly urls or create a outbound rule to rewrite the urls to show friendly urls to the user.

Using IIS manager

The easiest way to create friendly urls, the corresponding 301 redirects and outbound rules is by using the url-rewire add-in for IIS. If you don’t have the URL rewrite tool you can download it using the Web Platform Installer. If you don't have access to the IIS Manager you can place the code at the bottom of this page directly in the web.config file.

  1. Open your IIS Manager and highlight the website you want to apply the rules to.
  2. Double click url-rewrite.
    Url rewrite
  3. Click add rules from the right menu.
  4. Select user friendly url.
    SEO rule templates
  5. Give the rule a name.
  6. Enter an example ugly url.
  7. Select the format you want to use from the drop-down.
  8. Check the Create corresponding 301 redirect rule check box.
  9. Check the Create corresponding outbound rule check box.
  10. Click ok.
    Friendly url rule

That’s it all done. You can now look in your web.config and see the results.

In the web.config file

<system.webServer>
    ........
    <rewrite>
        <rules>
            <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                <match url="^$" />
                <conditions>
                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                        <add input="{QUERY_STRING}" pattern="^firstname=([^=&]+)&lastname=([^=&]+)$" />
                    </conditions>
                    <action type="Redirect" url="/{C:1}/{C:2}" appendQueryString="false" />
                </rule>
                <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
                    <match url="^/([^/]+)/([^/]+)/?$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="?firstname={R:1}&lastname={R:2}" />
                </rule>
      </rules>
            <outboundRules>
                <rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^(.*/)\?firstname=([^=&]+)&(?:amp;)?lastname=([^=&]+)$" />
                    <action type="Rewrite" value="{R:1}/{R:2}/{R:3}/" />
                </rule>
                <preConditions>
                    <remove name="ResponseIsHtml1" />
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
    </rewrite>
    ........
</system.webServer>