views:

1970

answers:

5

How do I rewrite URL's in ASP.NET?

I would like users to be able to goto http://www.website.com/users/smith instead of http://www.website.com/?user=smith

+14  A: 

Try the Managed Fusion Url Rewriter and Reverse Proxy:

http://urlrewriter.codeplex.com

The rule for rewriting this would be:

# clean up old rules and forward to new URL
RewriteRule ^/?user=(.*)  /users/$1 [NC,R=301]

# rewrite the rule internally
RewriteRule ^/users/(.*)  /?user=$1 [NC,L]
Nick Berardi
Dscoduc
That is a good idea and totally supported in this .NET version. The original intent was to make my example compatible with mod_rewrite which doesn't support that syntax.
Nick Berardi
This is awesome.
IainMH
+15  A: 

Scott Guthrie covers how to do this natively in .Net pretty extensively here.

I've used the httpmodule approach and it works well. It's basically what ManagedFusion is doing for you.

brendan
Thanks, the ScottGu blog was spot on!
Bjørn
+4  A: 

I have used an httpmodule for url rewriting from www.urlrewriting.net with great success (albeit I believe a much earlier, simpler version)

If you have very few actual rewriting rules then url mappings built in to .NET 2.0 are probably an easier option, there are a few write ups of these on the web, the 4guysfromrolla one seems fairly exhaustive but as you can see they don't support regular expression mappings are are as such rendered fairly useless in a dynamic environment (assuming "smith" in your example is not a special case then these would be of no use)

A: 

hi Nick Berardi,

i used the module of www.managedfusion.com/products/url-rewriter/, but i can't use it to rewrite .aspx pages to .html. can you please tell me how to do so ?

and if i have many querystrings in the url like mysite.com/home.aspx?a=123&b=456&c=789. how can i set it to be mysite.com/home/a/123/b/456/c/789 ?

also if any detailed documentation how to use this module 'd be great.

thanks in advance.

Website-Chef
+2  A: 

Microsoft now ships an official URL Rewriting Module for IIS: http://www.iis.net/download/urlrewrite

It supports most types of rewriting including setting server variables and wildcards.

It also will exist on all Azure web instances out of the box.

Sam Saffron
This looks interesting. Do you know if it will work on IIS6 and at what level in the network stack it sits?
GateKiller
This is a IIS7 and up solution only (its a module http://learn.iis.net/page.aspx/121/iis-7-modules-overview/), for IIS6 you will need www.isapirewrite.com or www.urlrewriter.net, isapi rewrite seems like a stable commercial product for iis6
Sam Saffron
@GateKiller :) forgot to notify you in my prev comment
Sam Saffron