Fix URL Canonicalization with ASP and not IIS

 In Web Marketing

URL canonicalization is a common technique employed by SEOs to resolve duplicate content issues associated with having the same page display for multiple versions of the URL.
The main issues occur when a websites homepage resolves on up to 4 different URLs such as

example.com/
www.example.com/
example.com/index.php
www.example.com/index.php

The problem shows 4 different URLs to the search engines which can dilute the effectiveness of links and also potentially cause a duplicate content issue.
The aim of URL canonicalization is to redirect the different URLs into 1 standard URL. For most cases this would be www.example.com/.

This is all relatively common knowledge for SEOs and many applications such as WordPress have it built in. If you use PHP/Apache then canonicalization is a relatively simple process using .htaccess where you will find plenty of guides.

Unfortunately not everyone uses PHP/Apache.  If you use ASP/ASP.net or god forbid PHP on IIS then things become a little more complex. Actually 301 redirects on IIS are quite easy but for this you need access to the IIS server, and for most people this is not possible.
If you need to do URL canonicalization in ASP or ASP.Net then you need to add some code to the top of each page (or call it in with an include)

For ASP

<%
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")
' 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
%>

For ASP.Net you need to use (via:x2line)

protected void Application_BeginRequest(Object sender, EventArgs e)
{
 HttpApplication app = sender as HttpApplication;
 string domainName = "example.com";
 if (app != null)
 {
 string host = app.Request.Url.Host.ToLower();
 string requestUrl = app.Request.Url.PathAndQuery;
 if (String.Equals(host, domainName))
 {
 Uri newURL = new Uri(app.Request.Url.Scheme +
 "://www." +
 domainName +
 requestUrl);
 app.Context.Response.RedirectLocation = newURL.ToString();
 app.Context.Response.StatusCode = 301;
 app.Context.Response.End();
 }
 }
}

It is worth noting that in the ASP example there is a small issue. The redirect doesn’t actually work for www.example.com/index.php to www.example.com as the value for “request.ServerVariables(“PATH_INFO”)” is the same for www.example.com/default.asp and www.example.com/ so trying to re-direct them will cause a to many re-directions error.

If there are any other guides to URL canonicalization guides for non standard scenarios (Such as Zeus Server!) feel free to post a link.

James
I am the director of Dolphin Promotions, a full service web design and marketing company based in Blackpool, UK.
Recent Posts
Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Start typing and press Enter to search