Reputation: 1290
Is there an easy way (preferably with htaccess and mod_rewrite) to force the browser to always access a site with the www. prefix (adding it automatically where necessary?)
Thx.
Upvotes: 3
Views: 3523
Reputation: 51
This is what I use:
$url= $_SERVER["SERVER_NAME"];
$page=$_SERVER["REQUEST_URI"];
if($url == "example.com"){
header("Location: http://www.example.com$page");
}
Upvotes: 5
Reputation: 13461
Rewritecond %{HTTP_HOST} !www.domain.com
RewriteRule ^/(.*)$ http://www.domain.com/$1 [R=301]
or maybe
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
Upvotes: 8