Summary
ContentsOne 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
ContentsBefore 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
ContentsI'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...
web.config
Contents<rewriter>
<rewrite url="~/(.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="~/$1"
processing="stop" />
<redirect url="~/(.*)/$" to="~/$1" permanent="true" processing="stop" />
<redirect url="~/cambia3/myuseragent(.*)" to="~/articles/64/what-is-my-user-agent"
permanent="true" processing="stop" />
<rewrite url="~/c4/(.+-.+-.+-.+-.+)/([^/]+)\.aspx$" to="~/Article.aspx?guid=$1&title=$2"
processing="stop" />
<redirect url="~/clicheweb/(.*)$" to="http://www.clicheweb.com/clicheweb/$1"
permanent="true" processing="stop" />
<rewrite url="^~/articles$" to="~/ArticlesList.aspx" processing="stop" />
<rewrite url="~/articles(\?.+)$" to="~/ArticleList.aspx$1" processing="stop" />
<rewrite url="~/login(.*)" to="~/Utility/Pages/Login.aspx$1" processing="stop" />
<redirect url="~/about$" to="~/articles/66/about-cambia-research"
permanent="false" processing="restart" />
<redirect url="~/ArticleList.aspx(.*)$" to="~/articles$1"
permanent="true" processing="restart" />
</rewriter>ScottGu's Trick for Postbacks with Rewritten URLs
ContentsEven 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?
ContentsLet 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.
Back to Top