Redirect Single Pages with Classic ASP

The Goal

If you have some ASP pages that you want redirecting to new URLs with the correct “301 Moved Permanently” status, this simple solution is easy to implement.

global.asa

URL redirects can be implemented using the global.asa file, but unfortunately that only works if the pages that you want to redirect actually exist on the server, so you can’t delete the old files and use global.asa to redirect requests.

The Solution

Place the following code at the top of your Classic ASP file and simply change the locations:

<%@LANGUAGE="VBSCRIPT"%>
<%
' Redirect to the new location with the correct 301 Moved Permanently status
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "http://www.example.com/new-page.php"
%>

I would expect that if you’re redirecting one page to an entirely new page there won’t be any actual page content in your first script, so there’s no need to use Response.End() to kill it after the headers are sent.

Canonical Redirects

Duplicate content can harm your website’s search engine ranking. If your pages can by found at both http://example.com/page.asp and http://www.example.com/page.asp this can be detrimental to your website.

By modifying the above Classic ASP code we can redirect visitors landing at http://example.com/page.asp to the correct content at http://www.example.com/page.asp

What we need to do is check the SERVER_NAME variable and if the “www.” part of your URL is not present then redirect the visitor to the same page but on the correct “www.” URL.

<%@LANGUAGE="VBSCRIPT"%>
<%
' If the domain is not www.example.com send the visitor to the correct domain with a
' 301 Moved Permanently status and append the correct page to the URL. Finally kill the
' script so that processing stops here
If (InStr(Request.ServerVariables("SERVER_NAME"),"www.example.com") = 0) then
   Response.Status = "301 Moved Permanently"
   Response.AddHeader "Location", "http://www.example.com" _
     + Request.ServerVariables("PATH_INFO")
   Response.End()
End If
%>

When programming canonical redirects like this it’s likely that your script will contain actual content rather than simply exist for the sake of the redirect. PHP scripts that issue redirects like this will continue to run until complete after issuing the headers. I can only assume that ASP is the same and so in this case Response.End() is added to prevent the script from proceeding after the redirect is issued.

Conclusion

Single page redirects in Classic ASP can be useful if your content has moved or if you want to make sure that visitors are viewing your canonical URL. If you can’t access IIS to set up the redirects and the global.asa won’t work, placing this code in your Classic ASP file will accomplish the redirect that you need.

See Also

Redirecting single pages with ASP.NET

3 Comments

  1. Been looking for a simple example to do this for a long time. Found many resources, but this is the only page that actually explains it well, and the code works perfectly.
    I would add that in order to redirect from “WWW” to “non WWW” (the opposite of the above example), you need to replace “www.example.com”) = 0)
    with
    “www.example.com”) = 1)
    and then remove the “www.” from “Location”, “http://www.example.com” _

    So the code for redirecting www to non-www is:

    <%@LANGUAGE="VBSCRIPT"%>
    <%
    If (InStr(Request.ServerVariables("SERVER_NAME"),"www.example.com") = 1) then
    Response.Status = "301 Moved Permanently"
    Response.AddHeader "Location", "http://example.com" _
    + Request.ServerVariables("PATH_INFO")
    Response.End()
    End If
    %>

  2. Kyle David

    Let’s say I want to move /x.asp into /content/x.asp. Now, when external links that have already existed remain pointing to /x.asp, I want to redirect /that/ to the new location. If I understand what you wrote, above, this means I need to keep an asp redirection /x.asp page, with only the asp code you describe in the first section above (updated accordingly, of course).

    I’d hate to have all that file “detritus” laying about, however, my hosting’s server does not allow me to add server redirects to the IIS server, so I have to do this via scripting.

  3. Barnaby Knowles

    Hi Kyle.

    Yes, I’m afraid that you need to keep /x.asp in place to redirect visitors to /content/x.asp for as long as people are still arriving at /x.asp

    Search engines should recognise the 301 redirect (moved permanently) and update their indexes to send visitors to /content/x.asp. Naturally you’ll update your own links to point to /content/x.asp but if anyone else is still linking to /x.asp you’ll need to ask them to update their links.

    I hope that helps.

Comments are closed.