Redirect Non www. Pages to www.

Home/Redirect Non www. Pages to www.

When building a website, from time to time it’s necessary to redirect the non www. version of the website to the www. version, especially since you’re then less likely to have two versions of your website in the Google SERPS.

And since a web client of mine is asking how this is done, I’ll share the most common method, a few simple lines in your .htaccess file in the website’s root folder.

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

Pretty simple, huh? And for completion, here’s the redirect from www. to the non www. version.

#REDIRECT TO NON WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} !^web-programmer\.com$ [NC]
RewriteRule ^(.*)$ http://web-programmer/$1 [R=301,L]

That’s really all there is to it. Tidy, useful and consistent. Everything a web development project should be.

2017-05-19T15:48:04+00:00 December 30th, 2014|Programming Tips, Web Programming|0 Comments

About the Author:

Warren Chandler is a freelance web programmer and web developer, based in Frinton on Sea, Essex. Warren specialises in web programming, web design, corporate identity, copy writing, SEO, logo and print media for home, work, and mobile platforms.

Leave A Comment