ASP.NET Web Dev: Map one directory to another? - asp.net

We are thinking of renaming our web directory schema to be more user friendly. However, we need any URL requests for the old directory structure to forward to the new one. so....
How do I forward requests for all of these:
http://mydomain.com/OLDdirname/
http://mydomain.com/OLDdirname/samesubdir/
http://mydomain.com/OLDdirname/samesubdir/samescript.aspx
to each of these respectively:
http://mydomain.com/NEWdirname/
http://mydomain.com/NEWdirname/samesubdir/
http://mydomain.com/NEWdirname/samesubdir/samescript.aspx
Any suggestions and perhaps some general guidance as far as gotchas?

You can use a number of things to do something like this. Basically, you're doing URL Rewriting. One of these products should help you get the job done:
ISAPI Rewrite
IIS Rewrite
opURL
IIRF

I use IIRF on many of my websites, and it has always worked without failing. You use regular expressions to define the rules, and it takes care of the rest. It would be extremely easy to setup a few redirects using IIRF.
http://cheeso.members.winisp.net/IIRF.aspx

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.

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.

Extensionless url rewriting / filtering url rewrites

I've done some url rewriting, always using the Intelligencia.UrlRewriter and it always worked fine. But now i want to achieve the following and somehow i can't get it right.
i want:
www.mydomain.com/products/books > rewrite to www.mydomain.com/products.aspx?id=books
this works fine, because i can set my criteria to /products/..
www.mydomain.com/mybook > rewrite to www.mydomain.com/productdetails.aspx?id=mybook
and of course i want www.mydomain.com/newbooks.aspx to function normally
and these last 2 don't work together.
is there a way i can tell my rewrite rule to only rewrite when there is no extension in my url? or is there an other trick?
Thanks in advance!
Well, a bit off the road, but try using ASP.NET Routing instead of URL Rewriter.
I was playing (and messing up) with the rewriter, and here on Stackoverflow I was advised to use Routing, and it is MUCH better (and easier when you get it).
You'll need some little changes to your ASPX files though, but very small.
I thought i'd solve this old, unanswered question. The problem with the extensionless urls turned out to be some configuration problem in IIS. Our system admin fixed this by allowing these urls and after that the rewriter had no problems picking up and translating the URLS.

is it possible to do URL re-write in asp.net?

I have developers working on a site for me, they have told me there is no possible way to re-write the URL so there is no second level category e.g.
Remove "/category" from this URL http://www.somesite.com/category/page.aspx
I understand that page.aspx can simply go in the root folder on the server however I don't want to do this as adding heaps of pages will slow down server load time
Do not believe them... It's easy and can be done for example by UrlRewriter.net library which is easy to use and open source. Also they can write own rewriter which is also not more then an hour of work, for example like here.
check whether it will helpful for your requirement: URL Rewriting in ASP.NET

implementing rewrite for http://site/NICK => http://site/profile.aspx?nick=NICK

hey guys, like the title says, how can i accomplish this sort of a rewrite as simple as possible? is it with httphandlers or perhaps the asp.net routing? i am using version asp.net 3.5.
the problem is that i want any other requests to be ignored, like requests to .aspx files, images, folders and such for example:
i want these to get handled:
site.com/john => site.com/profile.aspx?nick=john
site.com/mark => site.com/profile.aspx?nick=mark
site.com/fred => /site.com/profile.aspx?nick=fred
i DONT want these to get handled:
site.com/page.aspx
site.com/images/logo.jpg
your suggestions are appreciated :)
Scott Guthrie has a great post about this. If you're running IIS7, it's super easy with the URL Rewrite module.
this
Request.Url.ToString()
gets you the url (if the page i found this on is correct, i don't use asp.net very often)
you can use some regex on this URL, maybe some kind of...
site\.com/([\w])+$
don't know how to read the capturing parts of regex with .net, there must be an appropriate object.
then send a redirect to the user, you should be able to do this using the appropriate Redirect-HTTP-Header. if i'm right, there is a similarly named collection in the Response-Object.
note that this implementation does not allow rewriting
site.com/john/some_other_param
if you have questions on how to implement this in detail, i'll try to research further
regards
I've used UrlRewriter.net in the past to do this. It works really well. You can configure it to only apply the rewrite rules if there is NOT an existing file that maps to the url (ie, real aspx pages or image files)
http://www.urlrewriting.net
Of course, if you're using IIS7 there is built in routing to do this.

Resources