Home : Shared Hosting : Sitemap

WHATS INCLUDED:

Sunday, 8 March 2009

URL rewrites for older ASP websites

URL rewrites for older ASP websites

One of the biggest dilemmas on Microsoft based operating systems using Internet Information Services is URL rewrites. Even though the >NET framework does offer some tools for this for older technologies such as Active Server Pages or even HTML files there isn’t much to offer, there are third party applications that add ISAPI filters to perform rewrites much like Apache but you can perform rewrites buy using the IIS framework itself and custom error pages.

So how to perform rewrites using the error pages and Active Server Pages, well first you will need access to the custom error page settings in IIS or ask you host to change the following to map to your script that you can write explained here. Change the following error pages….

403;14 : Forbidden
404 : page not found
405 : Method not allowed
500;15 Internal Server Error

This means whenever a page cannot be found, or an error occurs (because of a global.asa entry), or even a permissions issue that it fires your custom ASP page. So now the server knows how to handle these errors lets give it the tools to do so…

Use the following below to construct a script to handle these events

<%@ Language=VBScript %>
<%
script = request.ServerVariables.Item("SCRIPT_NAME")
err_input = request.ServerVariables.Item("QUERY_STRING")

target = "newdomain.com/"
oldurl = " olddomain.com "

fixSTR = replace(err_input, oldurl,target)
fixSTR = replace(fixSTR , "404;","")
fixSTR = replace(fixSTR , "/:80","")

if instr(fixSTR, "403;") = FALSE then
else
fixSTR = "http://www." & target
end if

Response.Status="301 Moved Permanently"
Response.AddHeader "Location",fixSTR
%>

What this does is if for example you have http://www.olddomain.com/mysite.asp this would perform a 301 redirect to http://www.newdomain.com/mysite.asp.

How this is done is that IIS on error, loads the error page passing the requested resource as a query to the IIS engine to process the error, by grabbing this information, removing the error code and port and then constructing the URL with the correct domain you can build the new URL on the fly. Then by writing a new Status to the engine along with a new Header you can perform a 301 redirect. This is a cheap alternative to third party applications and a lot of server reconfiguration. Also this can be used on any IIS server without any .NET framework.

Of course you could build a case select routine to hardcode set mappings.

0 Comments:

Post a Comment



Links to this post:

Create a Link

<< Home


Username :
Password :