Categories: Web Marketing

Fix URL Canonicalization with ASP and not IIS

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 = "https://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 Smythe

Share
Published by
James Smythe

Recent Posts

Understanding Google PageSpeed Insights & Core Web Vitals – A green 90+ can still show as needing improvement in Search Console

Many website owners have breathed a sigh of relief that Google has delayed the algorithm…

2 months ago

Most Google searches generate no website traffic with zero-click searches

I briefly touched upon zero-click searches in a recent post about Google featured snippets. The…

4 months ago

Takedown bad Google My Business with a new tool from Google

Online reviews can make or break a business nowadays, which has unfortunately made reviews become…

4 months ago

Google featured snippets improve to normal levels following drop

Featured Snippets in Google are a blessing and a curse. One had they offer a…

4 months ago

LiteSpeed Cache + QUIC.cloud CDN vs WP Rocket + Cloudflare CDN PageSpeed Insights with Divi WordPress Theme

We all know website speed is important, ignoring Google; a slow website provides a terrible…

5 months ago

This website uses cookies.