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.
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.
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.
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.
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.