It is normal practice to have domains linked and accessed via the www version. However, if you prefer the non www version, just reverse the following:
To redirect all traffic from http://site.co.uk to http://www.site.co.uk (and also redirect all other domains parked into the same web space)
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.site\.co\.uk
RewriteRule (.*) http://www.site.co.uk/$1 [R=301,L]
To redirect http://www.site.co.uk/index.html to http://www.site.co.uk. Change to .htm or .shtml depending on your requirements and http://www.site.co.uk/index.php to http://www.site.co.uk
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.html
RewriteRule ^(.*)index.html$ http://www.site.co.uk/$1 [R=301,L] RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://www.site.co.uk/$1 [R=301,L]
How to do a 301 redirect
If you ever need to do a redirect in PHP code, make sure that you do more than just the default - ie not just the R, but the R=301 in the htaccess, or not only the "Location", but also the initial 301 Moved Permanently code. The default redirect is a non search engine friendly 302 temporary redirect.
A 301 redirect is a search engine safe way redirecting traffic.
In PHP
This PHP code must appear before any output is sent to the browser.
if(preg_match("/[index.php]$/",$_SERVER[REQUEST_URI]))
{
$url="http://".$_SERVER[HTTP_HOST].preg_replace("/(index.php)$/","",$_SERVER[REQUEST_URI]);
$sapi=php_sapi_name();
if (PHP_VERSION >= '4.3.0')
{
header("Location: $url", 0, 301);
}
else if ($sapi == 'cgi' OR $sapi == 'cgi-fcgi')
{
header("Location: $url");
// Call the status header after Location so we are sure to wipe out the 302 header sent by PHP
header('Status: 301 Moved Permanently');
}
else
{
header("Location: $url");
header('HTTP/1.1 301 Moved Permanently');
}
}