ASP.NET URL Rewriting - asp.net

How do I rewrite a URL in ASP.NET?
I would like users to be able to go to
http://www.website.com/users/smith
instead of
http://www.website.com/?user=smith

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]

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.

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)

Related

Use ASP.NET Routing AND Rewriting in same Project

i have a running Onlineshop which uses ASP.NET 4.5 and URL Rewriting via the web.config's rewrite-section.
As i am now adding some new parts to the shop i would like to use Routing (from System.Web.Routing) without changing the old (and running) code.
If i add some MapPageRoutes to the RouteCollection they never get fired as the Rewrites will fetch them and do the rewriting.I also added a ignore rewrite to the needed URLs, but this did not help.
Current Rewrites:
/test
/test/123
/test/123/edit
new Routes
/test2
/test2/123
...
Is the mixture possible within one Aebapplication and how can i get the Rewriting to ignore the Routes?
thank you for any hint
Christoph
You can use both theoratically without a problem. According to iis.net
The technologies (routing and url rewriting) can be used together and can complement each other.
However, it is not recommended because it may cause conflicts as some implementations of ASP.NET URL Routing internally use rewriting as well.
Here you can find how to combine routing and rewriting together. And rewriting does not have to ignore routing, because you should define different conditions for each. My personal recommendation would be convert rewriting rules to routing because you will already perform some effort and how many rules could exist anyway.

Microsoft IIS Url Rewrite 2.0 - rewrite url with x number of folders

I'm trying to set up a url rewrite rule using Microsoft IIS URL Rewrite 2.0, I'm running IIS7 and .NET 4.
I want to match any number of a specific pattern, for example:
www.test.com/test1/
www.test.com/test1/test2/
www.test.com/test1/test2/test3/
www.test.com/test1/test2/test3/test4/ etc...
and [respectively] rewrite the results as
www.test.com/page.aspx?q=test1
www.test.com/page.aspx?q=test1&q=test2
www.test.com/page.aspx?q=test1&q=test2&q=test3
www.test.com/page.aspx?q=test1&q=test2&q=test3&q=test4 etc...
or
www.test.com/page.aspx?q=test1
www.test.com/page.aspx?q=test1,test2
www.test.com/page.aspx?q=test1,test2,test3
www.test.com/page.aspx?q=test1,test2,test3,test4
This is a fairly straight forward thing to do when using the standard c# regex engine (I have considered creating a custom HttpModule to handle this and iterating over a Match collection), however I wanted to know if it was possible to do with the IIS url rewrite component as I'm using this quite happily for other parts of the site.
Any ideas?
Why don't you just match and pass in
www.test.com/page.aspx?q=test1/test2/test3/test4
Then it would be up to page.aspx to parse it how it needs it? That way your configuration won't have to change when it really is application logic.
If you're using .net 4.0, what not use Routing?
http://everymanprogrammer.com/index.php/asp-net-4-0-routing-a-simple-tutorial-that-works/

URL aliases: rewrite, routing, not sure

ASP.net 4.0 / IIS7.
I want to "alias" a single web form to appear as various extensionless urls. Given a form in my web root called "mySite.com/ColorWebForm456.aspx":
I want it served as multiple names, e.g., mySite.com/Color, mySite.com/Colour, mySite.com/Colors, mySite.com/Coler, etc., without creating folders and duplicate forms with those names.
I never want mySite.com/ColorWebForm456.aspx displayed in the browser, it needs to display as mySite.com/Color, even if the user somehow acquires it and types in that exact ~.aspx address.
The variations will account for several alternate or mis-spellings users might attempt - I don't want them "corrected", however. So, if a user types in mySite.com/Colour, the url is NOT rewritten to mySite.com/Color, but the same page is served via ColorWebForm456.aspx as the requested "mySite.com/Colour".
I've seen so many articles on this that I'm not even sure where this would be best handled: in Global.asax, IIS7 URL Rewrite, web.config, etc., and I'm not even sure this is technically a case of url rewriting or routing... ?
You can achieve this with ASP.NET 4 routing for web forms, or if you are using MVC its available too: http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
Except potentially for #3; it would work, but you would have to explicitly declare the variations you want. In that case, rewriting supports regular expression matching, so that might be better for what you are looking for.
I had issues setting up URL rewriting and various aspects of web forms, so be aware. There is some setup required for a rewrite module potentially (I could have done it wrong too), where URL routing is already built in and handled.
HTH.
If you have a rewrite engine available:
(using ISAPI Rewrite .htaccess syntax)
# redirect any requests for the filename back to the friendly URL
RewriteRule ^/colorWebForm4567.aspx(.*) /Color$1 [NC,R=302]
# rewrite /Color requests to the web form
RewriteRule ^/(Color|Colour)/(.*) /ColorWebForm4567.aspx$2
Following article expalins it all, but if you will be using a web host after you complete your application I would check with the hosting provider first to see if they are offering everything you need to have:
http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
Hope this helps!
As a side note: Search engines dislike it, if you publish content under different URLs ("duplicate content").
So my recommendation would be to stick with one (rewritten) URL, not multiple (rewritten) URLs for the same content.

IIS7 URL Rewrite with dynamic subdomains

My goal is to implement the following scheme using the subdomain and path as attributes:
Use a wildcard DNS entry that routes any subdomain to the root site:
Example:
*.example.com
ex: http://xyz.example.com
to
http://example.com
Next I want to rewrite the requests to point to a specific page, passing both the subdomain and the request path as attributes.
Example:
http://xyz123.example.com/images/header.jpg
to
http://example.com/get.aspx?id=xyz123&path=/images/header.jpg
I've seen several questions on here regarding similar goals, but not quite the same. I'm new to using rewrite rules, so any help is appreciated. I will update this as I make progress.
for IIS7, url rewrite functionality is built-in. Rules are set in web.config. For IIS6 you need an ISAPI dll that does the same for you. Use IIRF, it works just fine.

Subdomain to point to subdirectory of main site?

I have my website running at:
www.example.com
I also have this other domain:
www.example.net
Is it possible to point "www.example.net" to a subdirectory of "www.example.com"? For example:
www.example.net-> www.example.com/other
?
It's not just a redirect, I need to serve the pages and see "www.example.net" in the user's browser.
What do you think?
We use IIS7 and everything is ASP.NET (aspx pages, ascx controls, etc).
Using ISAPI Rewrite
[ISAPI_Rewrite]
RewriteCond Host: (?:www\.)?other\.com
RewriteRule (.*) http://www.domain.com/other$1 [I,L]
You're looking at two methods for this:
URL rewriter
Server.Execute method
The first is a httpmodule that silently redirects the request without changing the url in the address bar, the second is a method you can call within aspx code. I think the latter is only good though if you're working in the same application, so I would go with the URL rewriter.
There is an official iis7 URL rewriter available on www.iis.net afaik.
-Oisin
Two questions for you:
Can you set them up as two different web sites in IIS?
If someone visited example.net/page.htm, would that be the same as example.com/other/page.htm?
If the answer is yes to both of those, why not just setup a new web site in IIS and point it to the "other" file path.

Resources