DCD
DCD

Reputation: 1290

Forcing the www prefix with PHP/htaccess/mod_rewrite

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

Answers (2)

Daniel
Daniel

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

jasonbar
jasonbar

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

Related Questions