Introduction

You are setting up a website?
In more cases you will experience an odd situation where some of your links are working and some not or links starting with wwww are working but starting with http:// are not working and some might experience www is working but http is not.

This happens because apache (webserver) treats http://something.com and http://www.something.com differently, it assumes that www is subdomain of something.com.
In other words when you type http://www.something.com apache assumes you want to access subdomain “www” of domain “something.com”

www is so commonly used that most Registrars and Hosting providers do automatically create subdomain named with www for you and permanently redirect it to the web server hosted with them, but redirection is not always happened, some time you yourself have to manage this redirection.

To manage redirection we normally use the apache config file (.htaccess) which is independent to each site, in other words each site normally have its own apache config file.

non-www or http to www redirection

You http links are not workin? but links starting with www are working fine? or you need to redirect all non www links to www?

To redirect all non www links to www, open the .htaccess files in your site root directory, if you don’t find that file please create one.

Add following to you .htaccess file

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Save the file.

Now all links witch don’t have www will be redirected to www… for example http://something.com will be redirected to http://www.something.com

Redirect www to non www or http only

Do you want to route all www links to non-www or http only?

Add following to you .htaccess file

RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]

Save the file.

Now all the links starting with www will be routed to non-www or http only, for example http://www.something.com will be redirected to http://something.com.

Advertisements