Rewrite Redux

by Matt 15. June 2009 22:10

For anyone who has ever used UrlRewriter.NET, I'm sure you have had a similar problem.

I recently integrated Intelligencia.UrlRewriter into a project to take care of some SEO issues that were present.  This worked like a charm when running the dev instance locally, but when it was deployed to our test server, it was no longer working.  It was returning 404's for each of the rewritten url's.

Finally, after spending too much energy on this issue and reading results upon results returned from Google searches, I discovered the solution to the problem.  The Regex in the web.config must end in a file extension .  Locally, it worked without, but I could not get it to work on the remote server without an extension.  

Here is the following you will need to be sure of in order to make your rewriter experience as painless as possible:

  • First, add a reference to "Intelligencia.UrlRewriter.dll" and make sure it copies to your bin directory when compiling.
  • Next, after your system.web section is terminated, put in your rules.  Note the .aspx extension in the regex.
<rewriter>
  <rewrite url="~/ArticleList/(\d+)/(\d+)/(.*).aspx" to="~/Articles/Articles.aspx?ArticleSourceId=$1&amp;Page=$2"/>
  <rewrite url="~/Article/(.*)/(\d+)/(.*).aspx" to="~/Articles/Post.aspx?PostID=$2"/>
</rewriter>
  • You will also want to add the following to your configSections tag:
<configSections>
  <section name="rewriter" requirePermission="false" 
           type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
  • Finally, you will need to put the following entry in your httpModules section:
<httpModules>
  <add name="UrlRewriter" 
       type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
</httpModules>

I hope that this can help somebody else from spending a similar amount of energy on this problem

Categories: .NET | C#