Friday April 29th

Thursday April 28th

Wednesday April 27th

Url Rewriting in ASP.NET (ScottGu)

People often ask me for guidance on how they can dynamically "re-write" URLs and/or have the ability to publish cleaner URL end-points within their ASP.NET web applications. This blog post summarizes a few approaches you can take to cleanly map or rewrite URLs with ASP.NET, and have the option to structure the URLs of your application however you want.



I've applied URL rewriting and appear to have lost my relative addressing, as per previous post -I've used " ~/" to acess things from root .

However a rewritten URL from : to

The "rewriting" correctly points to the product page, however it appears to be including or "cloths-C3" as the root directory.

This effectively upsets my refences to ~/images and css and other goodies as the root is now incorrectly appending the translated directory.

I had unchecked the "must exist" box in IIS5 and I"d very much appreciate it if anyone could suggest what my error is.

Thanks very much

When testing my url rewriting rules I use a simple redirection like;

<rewrite url="~/.*-P([0-9]+).html$" to="~/Product.aspx?ProductID=$1" procesing="stop" />

This results in the correct page rewrite delivering as expected e.g.

http://localhost/2Die5/Beautiful-Cami-P19.html showing as the address but the actual page rendered is the original URL,
http://localhost/2Die5/Product.aspx?ProductID=19 being executed with predictable results.

All the page behaviours including "root" relative address provided anticipated results, e.g.
NavigateUrl="~/Registry.aspx" results in a redirection to http://localhost/2Die5/Registry.aspx which is perfect.

The next step is where I am struggling, for example

When using the following redirection;

<rewrite url="~/.*-C([0-9]+)/.*-P([0-9]+).html$"
to="~/Product.aspx?CategoryID=$1&amp;ProductID=$2" procesing="stop" />

http://localhost/2Die5/Bustiers-C1/Beautiful-Cami-P19.html provides the anticipated Prodiuct.aspx page however the root directory appears as having changed.

The use of NavigateUrl="~/Registry.aspx" results in a 404 with the following error message;

Requested URL: /2Die5/Bustiers-C1/Registry.aspx

which suggest that application of rewrite rule has effectviely appended the "Bustier-C1" onto the original root directory. This is a pervasive issue with other controls and css addressing experiencing the same issue.

I'd greatly appreciate some assistance in resolving this issue.

I'm using "Approach 2: Using an HttpModule to Perform URL Rewriting"
i'm confusing how to handle 404 pages,

<error-handler code="500" url="/oops.aspx" />
<error-handler code="404" url="/not-found.html" />

for these codes above, where can i put in web.config page?
One more question, i do not have permission to access to IIS configuaration, so some url like
will be redirect to 404 pages.
how can i set up a rule for it?


Hi Scott,

Tried approach # 2, missing something, tried using HttpModule following blog posted by Magdanurov in it worked locally (IIS 5.1), when deployed it at Live server (IIS 6) it stop working...Any ideas why and how to fixed this?? Is there need to configure?? Tried using Intelligencia.UrlRewriter it gives me Error 404?? Same error happens in Live server..


Commenting on Stories is limited for now and will open up to those recommended by the community. Learn how
Loading DotNetKicks...
brought to you by the Kicks Network