Nuthin' but .NET

URL Rewriter for .NET - Examples

An easy approach for URL redirection and rewriting in your ASP.NET applications

Summary

One of the few third party tools, open source or otherwise, that I used in creating this site is URL Rewriter for .NET. It's an excellent tool, and even though its website has decent documentation, it was sparse on examples of how to use the tool in practice. I also ran into a problem with ASP.NET postbacks when rewriting URLs. Below I present several real life examples of how I use URL Rewriter along with info on fixing the postback URL problem.

A Great Free 3rd Party Tool

Before we get to the meat and potatoes, I want to take a moment and offer some praise for URL Rewriter. I'm not generally a fan of third party code whether it's freeware, open source, commercial or something else. The sad fact is that 95% of software and software tools are not very good. They are generally buggy, inflexible or just more trouble than they're worth. However, once in while I run across a tool that just works. It's easy, flexible, well designed and relatively bug free. URL Rewriter is one of those tools. So, I highly recommend it for basic URL rewriting. Read on for more tips.

URL Rewriter Features

I'm only going to address redirecting and rewriting URLs in this article. It's really all I needed. But URL Rewriter also supports

  • Including query string parameters in the user friendly URL but convert them to the true query string for your ASPX page.
  • Block users based on user agent. This could block misbehaving crawlers.
  • Ban users based on IP range
  • And more...

Sample Web.Config

web.config
<rewriter>
   <!-- 
      Don't try to rewrite images and other basic file resources.  Just mirror 
      the URL.  Put this first.  
   -->
   <rewrite url="~/(.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="~/$1" 
         processing="stop" />

   <!-- 
       Redirect anything with a slash at the end.  This prevents 
       the possibility of duplicate URLs for the same content which can
       potentially be penalized by search engines.  
   -->
   <redirect url="~/(.*)/$" to="~/$1" permanent="true" processing="stop" />

   <!-- 
      Redirect old URLs to the new URL.  Note the (.*) at the end of the url pattern.
      Just in case there is space or a query string I capture that.  However, I drop
      that extra text in the to attribute.  It is just a catch
      all trick.  
   -->
   <redirect url="~/cambia3/myuseragent(.*)" to="~/articles/64/what-is-my-user-agent" 
         permanent="true" processing="stop" />

    <!-- 
       Redirect old style URLs to the new style
        Old style: /c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
        New style: /articles/15/javascript-char-codes-key-codes
    -->
    <rewrite url="~/c4/(.+-.+-.+-.+-.+)/([^/]+)\.aspx$" to="~/Article.aspx?guid=$1&amp;title=$2" 
          processing="stop" />

   <!-- Permanently redirect URLs which have moved, using an HTTP 301 redirect -->
   <redirect url="~/clicheweb/(.*)$" to="http://www.clicheweb.com/clicheweb/$1" 
         permanent="true" processing="stop" />

   <!-- Rewrite /articles -->
   <rewrite url="^~/articles$" to="~/ArticlesList.aspx" processing="stop" />

   <!-- Rewrite /articles with query string -->
   <rewrite url="~/articles(\?.+)$" to="~/ArticleList.aspx$1" processing="stop" />

   <!-- Rewrite shortcut URLs including any query string or fragment and point to a subfolder -->
   <rewrite url="~/login(.*)" to="~/Utility/Pages/Login.aspx$1" processing="stop" />

   <!-- Rewrite shortcut  -->
   <redirect url="~/about$" to="~/articles/66/about-cambia-research" 
         permanent="false" processing="restart" />

   <!-- 
      Prevent users from directly accessing the actual ASPX page.
      Note the restart value. It will re-process all the rewriter 
      commands without returning to the client browser.
      Put these items last. 
   -->
   <redirect url="~/ArticleList.aspx(.*)$" to="~/articles$1" 
         permanent="true" processing="restart" />



</rewriter>

ScottGu's Trick for Postbacks with Rewritten URLs

Even though you have successfully used a rewritten URL to access your web page, .NET will still write the form action attribute in the HTML as the path of the actual ASPX page. For example, suppose your rewritten URL is http://www.cambiaresearch.com/pages/16/cool-stuff. ASP.NET would render the HTML like this

<form method="post" action="/MyPage.aspx?id=16&title=cool-stuff" id="ctl01">
   <div class="aspNetHidden">
   <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" ...

   ...[lots more HTML here]...

</form>

See the problem? The friendly URL and the postback URL do not match. You need these to match for everything to work right.

ScottGu posted a simple, elegant solution to the URL rewrite postback problem. It's the 2nd to last section in the article. In short, it uses the Control Adapter extensibility architecture. Just follow the instructions, it's much easier than it sounds.

Was This Helpful?

Let me know if this was helpful or if you have other questions. I will try to update the post if you have some good questions or comments.

Also, I'm sure you all have some other great tricks when using URL Rewriter. Please feel free to share them in the comments.

 

Version: 6.0.20200920.1535