Category Archives: asp

Prepend “www.” to URL

Published / by Joe Johnson

Even the setup of this very blog was wrought with an un-documented issue with URL rewriting. Most SEO experts worth their titles know that Google and other search engines recognize “” and “” as two distinct sites and split your results and ranking between the two. The ultimate goal is to try and combine your traffic down to one distict URL and prevent dispersing your rank. In the case of my blog, I chose to redirect all traffic to, which means if someone types some url without a www, it should be redirected automatically.

A simple redirect will take any URL containing and send it to This is not optimal if someone is trying to get to my Contact page ( and is instead sent to my home page ( A context-sensitive redirect will maintain any trailing path on the URL when redirecting to the new host (so becomes From a systems-management perspective, the simple redirect is far easier to implement with three lines of code in my header. However, from a user-experience perspective, the latter option is 1000 times more efficient and friendly, but it requires coding on every page of my site or configuration changes to my web server. Below you’ll find options for how to implement this change for either your web server or in the code of your site.

Code Approach: ASP

For ASP, merely add the following to the top of every ASP page in your site, or paste it to a single file and include it before the <html> tag in each of your pages:

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("REQUEST_URI")
End if

Code Approach: PHP

For PHP, merely add the following to the top of every PHP page in your site, or paste it to a single file and include it before the <html> tag in each of your pages:

if(strpos($_SERVER['SERVER_NAME'], "www") === false) 
	header("HTTP/1.1 301 Moved Permanently");
	header("Location: http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

Server Approach: Apache

In order to redirect to the www URL in Apache, you must install and configure the mod_rewrite plugin. Once that is complete, create a new .htaccess in the root of your site and add the following lines, where is your domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule (.*)$1 [R=301,L]

Server Approach: IIS

By default, IIS 6 and earlier did not come with a URL rewriting utility. Helicon has a product named ISAPI_Rewrite for sale that does an excellent job and uses most of the same syntax as mod_rewrite in Apache. The correct httpd.ini configuration for ISAPI_Rewrite 3.0 and later is:

RewriteEngine on

RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]


That about covers it. If you have difficulty implementing any of the solutions above, please feel free to contact me or leave a comment.