This website stores cookies. Click here to accept them.cookie information page

"seo" category

www and non-www

28th December 2011
Categories: php, seo

While most websites serve up exactly the same web pages on the www and non www versions ( and it is good practice to have one 'canonical' version of your website and divert the other version to it, as search engines have traditionally viewed the two versions as duplicates (which isn't good) and having one route to a 'resource' narrows down the margin of error if there are changes to the way you serve up web pages.

In php you can divert one to the other using the following code:

// redirect to www if not there already
  && (strpos($_SERVER['HTTP_HOST'],'')!==false)
  Header( "HTTP/1.1 301 Moved Permanently" );

If you have access to the DNS for your website you may wish to point the non-www site at a different location altogether, with just one index.php file containing the following code:

Header( "HTTP/1.1 301 Moved Permanently" );

Note that the header containing the 301 code is important for search engines; in almost all cases you should use a 301 (permanent) redirect as it tells search engine spiders that the old url is redundant and the new url should be credited with any 'kudos' that the old url ever had.

A slightly neater way of doing the redirect is to do it outside of PHP code, in your .htaccess file if you are running on apache and you have permissions (via the apache config) to do it:

# rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# non www to www
RewriteCond %{HTTP_HOST} !^www\.mygreatwebsite\.org$
RewriteRule (.*)$1 [R=301,L]

On IIS you can do it using the website properties within IIS manager.  Just add another website for the non-www version, and once added, in the website properties, instead of serving web pages from a folder on your computer, choose to make it a 'redirection to another URL'.  Remember to take the non-www version off the host headers accepted by the www version (website... advanced button).

© Alberon Ltd 2018

8 Standingford House
26 Cave Street

01865 596 144

Oxford Web is a trading name of Alberon Ltd, registered company no. 5765707 (England & Wales).