EcommerceTemplates and Proper 301 Redirects in ASP
Warning: copy() [function.copy]: Filename cannot be empty in /home/jlhdes/public_html/wp-content/plugins/mytube/mytube.php on line 220
Update 8/31/06 the following codes can now be downloaded for the products, categories, and proddetail pages for 301 redirects on ecommerce templates in asp.
I love the ecommercetemplates shopping cart solution, but there is one thing that has annoyed me since I started working with them. If you have to remove a product, as often happens, the user sees a basically blank page with the note:
Sorry, this product is not currently available
Not very appealing, and doesn’t really help your customer much. Much worse however, is that if that product was indexed by a search engine and the page had some ranking, you’ve lost all value. The same message is also given if someone accidentally links to a non-existent product, or makes a mistake in the link. Often manufacturers will change a product and upgrade it to something better and you have to remove the old product and add the new product, also loosing all of your search engine credit.
We need a way to tell Google and the rest that the product the searcher was looking for is no longer available, but can be found at this new location. When we tell them that all value the old page should be saved and moved to the new page.
The 301 redirect accomplished just that mission, but now I needed to find a way to do it with the current database structure so that it was automatic. By modifying your proddetail.asp page with the following code you will have accomplished a proper 301 redirect under three different occasions.
- The first part deals with canonicalization. If someone reaches your site at http://yoursite.com but you really want them to use http://www.yoursite.com we redirect them to the right URL. Since you probably have dynamic pages that pass on parameters, this code accomplishes that as well. As a matter of fact this should be placed on the top of every .asp page you’ve got, I’ll show that separate code later.
- If you have moved a product or no longer offer a product, but would like the visitor to go to the best available choice, they will be automatically forwarded. The search engines will also transfer all value and links to the new page.
- If someone mistakenly types in a wrong url, or a bad link sends them to a product that doesn’t exist we will be able to send them to some navigation structure.
The following code should be placed at the top of your proddetail.asp page below the includes for the database connection and metainfo. It assumes that you are using meta descriptions and titles, and if you are not, do it now! Your search engine rankings will thank you.
<%
'This code is provided as-is with no guarantee or warranty
'It is provided for free distribution as long as these comments
'are left in place.
'Designed by www.jlh-design.com 2006
Dim Domain
Domain = lcase(request.ServerVariables("HTTP_HOST"))
If productname= "301" then
Response.Clear
Response.Status ="301 Moved Permanently"
Response.AddHeader "Location","http://" & Domain & "/proddetail.asp" & "?prod=" & productdescription
Response.End
end if
If productname= "" then
Response.Clear
Response.Status ="301 Moved Permanently"
Response.AddHeader "Location","http://" & Domain & "/categories.asp"
Response.End
end if
%>
After that is done, we need to edit the products.
If you simply delete a product they will be sent to your categories page. However, if you’d like to give them a better choice and send them to a suggested product instead open up the product that is no longer available:
- Leave the product ID (Reference) alone
- Change product status to not display
- Change the product name (Prod Name) to: 301
- Change the product description (short) to the product ID of the product you want to send them to.
- Save the product
Now visit the old product url, it should automatically send you to correct product.
Just to make sure everything worked Check Your HTTP Headers by entering the old product URL, you should see a 301 redirect to the new page, then a 200 at the new page. While your on that site be sure to check the other redirects. Try the URL without the WWW, and then try going to a product detail page for a product that doesn’t exist.
For the other asp pages on your site, you’ll want to redirect those as well to the www version, the following code should be entered before the first “HEAD” tag. See this post about the complete explanation of 301 redirecting for canonicalization and downloads including to the non-www version.
<%
'This code will redirect any asp page to the www version
'This code is provided as-is with no guarantee or warranty
'It is provided for free distribution as long as these comments
'are left in place.
'Designed by www.jlh-design.com 2006
Dim Domain_Name, theURL, QUERY_STRING, HTTP_PATH,TEMP_NUM
'Get domain that the page is on
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
'Check if URL is the www version
if left(Domain_Name, 3) <> "www" Then
HTTP_PATH = request.ServerVariables("PATH_INFO")
'Check if page is default.asp if so, redirect to "/". If other index page is used, such
'as index.asp the numbers in the right and len statement need to be changed, as well
'as the IF statment to indicate the index page.
If right(HTTP_PATH, 12) = "/default.asp" Then
TEMP_NUM = len(HTTP_PATH)-11
HTTP_PATH = left(HTTP_PATH,TEMP_NUM)
End If
' Sets the new URL settings with correct page
QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://www." & Domain_Name & HTTP_PATH
'This section passes on the query string variables
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if
' Send 301 response and new location
Response.Clear
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
Response.Flush
Response.End
end if
%>

