We are replacing an old classic asp website with a .NET 3.5 solution.
We need to redirect all of the classic ASP requests to aspx pages (i.e. contactus.asp, may now route to /contact-us/default.aspx). What I woudl like is for the requests to hit global.asax so I can do something like
If url == "bob.asp"
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", SiteConfig.SiteURL + redirectUrl);
End If
There are two inelegant solutions.
A) Place a global.asa file and do the routing through that.
B) Map asp files to the .NET engine. Great, but then if we need to host classic asp sites on our sites IIS will be sending the requests to the wrong place.
I found a nice solution here
http://forums.asp.net/p/1202225/3458901.aspx
Which stated something like this may work...
<buildProviders>
<add extension=".php" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
<httpHandlers>
<add verb="*" path="*.php" type="System.Web.UI.PageHandlerFactory" validate="True" />
</httpHandlers>
This example was for php but I assume the same thing would work for asp. However after changing .php to .asp in the example and placing the tags in the correct part of the web.config I'm having no joy (a 500 server error actually).
Can anyone shed any light on this or give me an elegant solution.
Had a feeling the above solution wouldnt work for php or asp as IIS will have routed the request before it gets to the .NET engine.
Thanks in advance
Steve
Eduardo Molteni's answer works, except for one thing. It actually passes the browser a 302 instead of a 301.
I believe instead of:
it should be:
The response.redirect is basically interrupting what you were setting up with the Response.StatusCode and the browser ends up getting a "302 Found".
I'm not really sure how search engines handle a 302 vs a 301 so maybe its not an issue. It seems to me though that a true Permanent Redirect (301) would be preferred.
For those on v4 of the framework, there seems to be a new option:
I haven't tested this, but I'm assuming it provides a 301 as the status code. Details here: HttpResponse.RedirectPermanent
Big edit: I was pointed by @EdSF in the comments that the answer was wrong. In disbelief I checked using Firebug, and, in fact, it was wrong.
You need to use
Context.Response.RedirectLocation
for the status code to work.I'm doing the same in global.asax:
The only thing you have to do if you are using II6 you have to configure this ISAPI filter in this way:
The file is
c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll
The simplest thing is to use a custom 404 error page at the IIS level. This page can be any ASPX page; you have access to the original request via the HttpContext.
In IIS 6.0, you'll notice that by default URLs mapped to the .NET engine (.aspx,,asmx etc) use their own 404 handler defined in web.config. No matter; you can cause even those pages to be sent to the IIS 404 page if you edit the ASPX engine mapping and ensure that the tickbox for "file must exist" is set. This redirects all boken links to the IIS 404 handler, even if they are mapped to .NET handlers. People make the mistake of trying to redirect all broken links to the web.config defined 404handler; it's must easier if you just force all to go to the IIS defined one instread.
I use a modified version of the Smart 404 Handler script from http://evolvedcode.net/content/code_smart404/. I added code to do custom mapping based of a table in our database.
This script could easily be re-written in ASP.NET and then mapped the same way.