Fixing Canonical Issues On Linux And Windows Servers
I figure’d I’d write this one as a resource for whoever might need to quick look up how to do this. I know there are a lot out there, but few go talk about anything other than a .htaccess for an Apache server.
If you’re in the SEO game, all you need to know when somebody mentions canonicalization is “www vs non www”. There’s an entire wikipedia article you can read about it, but basically it will hurt your head unless you have a lot of patience to sift through crap that doesn’t affect you in any way.
Apache Canonical Redirect
Let’s start with the basic Apache 301 redirect. Say your site is http://whatever.com, and you want it to always be http://www.whatever.com. You would open up your .htaccess (or create one in notepad if you don’t already have one) and insert this line of code.
# mod_rewrite in use
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^whatever.com [NC]
RewriteRule ^(.*)$ http://www.whatever.com/$1 [R=301,L]
There, just upload that to your root and bingo bango you’re a freaking computer genius!
ASP Canonical Redirect
Ok, now for something that has hurt my head because I hate windows servers and everything associated with them. If you want to set a canonical version of your site in ASP. After scouring around the web I found a solution at seoimage.com that promises to hold the solution.
Insert this code into a global include file on your website.
<%
If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www."
& Request.ServerVariables("HTTP_HOST")
& Request.ServerVariables("SCRIPT_NAME")
End if
%>
Solution 2 for ASP 301 Redirect from here
<%
' If the server name is not www.sitename.com we can do the redirect to www.sitename.com.
' The only time we can is if the method is a GET
' (no way to pass along the POST arguments) and its on port 80 (don't want to redirect the SSL).
if ( strcomp( lcase( Request.ServerVariables("SERVER_NAME") ) , "www.sitename.com", 1 ) <> 0 _
AND Request.ServerVariables(”SERVER_PORT”) = 80 _
AND strcomp( lcase( Request.ServerVariables(”REQUEST_METHOD”) ) , “get” , 1 ) = 0 _
) then
URL = “http://www.sitename.com” & Request.ServerVariables(”SCRIPT_NAME”)
if len ( request.servervariables(”QUERY_STRING” ) ) > 0 then
URL = URL + “?” + request.servervariables(”QUERY_STRING” )
end if
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, URL
Response.End
end if
%>
ASP.NET Canonical Redirect
For those using ASP.NET technologies, the thing to do, according to my sources, is to either use the aformentioned code or access the IIS Internet Services Manager and right click on the folder you would like changed and select “Redirection to a URL” while checking two fields: “Exact URL mentioned above” and “A permanent redirection for this resource” and then just apply it and close it out.
Now, I’m very interested in setting canonical versions of ASP websites, especially since as an agency SEO, I frequently don’t have access to the IIS Internet Services Manager and cannot resolve these issues on my own. So I would love to hear any feedback on this.


