Forcing www (or no-www) in domain name
One of the first things I do when I begin implementing a site is to force the www (or no-www) in the domain name. Why would I want to do this? For search engine optimization of course. For example, naseerahmad.com redirects to www.naseerahmad.com. What this means for Google is that it gets crawled as one site. Sites that link to naseerahmad.com will add to the pagerank of www.naseerahmad.com. Theoretically, this could double your Pagerank score (but not necessarily your Pagerank rating).
Anyway, here is the code to add to your .htaccess file to force www, but make sure mod_rewrite apache module is turned on:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.naseerahmad\.com$ [NC] RewriteRule ^(.*)$ [R=301,L]
And here is the code to force no-www (in spirit of no-www.org):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.naseerahmad\.com$ [NC] RewriteRule ^(.*)$ [R=301,L]
Of course, replace naseerahmad.com above with your own domain name. To edit your .htaccess file you may need to enable view hidden files, or if the file doesn’t exist, you’ll have to create it yourself. The best way is to do it through an SSH connection to your server.
If you don’t have access to .htaccess, you could also accomplish the same thing through php with a simple script at the top of every page that will try to find www in the URL and redirect if found. Not the most elegant solution, but it does what it needs to do for those who can’t edit .htaccess for some reason.
if(!substr($_SERVER[‘HTTP_HOST’], “www”)) //check if www exists in URL
{ header(‘Location: http://www.naseerahmad.com/’); //redirect to www.naseerahmad.com
}
Or for those using wordpress, you could simply install a plugin that does all this for you! One example plugin is WWW-Redirect.
Thanks and waiting for your feedback.