Globally switching file extensions from htm to asp

If you need to switch a site from htm to asp without losing search engine ranking (for example: moving from a static site to an updateable cms driven site) there's a simple way of achieving this.

Using a 301 redirect is recognised as the most search engine friendly method for webpage redirection. Combining this with a custom 404.asp page to globally redirect all requests for .htm pages to the .asp equivalent would seem the ideal solution.

If a user attempts to view a page with the extension .htm, they're redirected to your 404.asp page together with the URL they entered.

The URL is added to the querystring in the format:


All you need to do is add a few lines of code to the top of your 404.asp page to retrieve the querystring, strip the "404;", replace the file extension and add the asp 301 redirect.

Dim strQuery, strRedirectURL
strQuery = Request.ServerVariables("QUERY_STRING")
If Right(strQuery, 4) = ".htm" Then
strRedirectURL = Replace(strQuery, ".htm", ".asp")
strRedirectURL = Replace(strRedirectURL, ":80", "")
strRedirectURL = Replace(strRedirectURL, "404;", "")
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", strRedirectURL
End If

Once your 404.asp page is ready, you need to change all your page extensions from .htm to .asp (and all your internal links on all your pages too!) and then upload your renamed pages and your new 404.asp page to your server.