开发者

Forcing the www prefix with PHP/htaccess/mod_rewrite

开发者 https://www.devze.com 2022-12-24 15:34 出处:网络
Is there an easy way (preferably with htaccess and开发者_如何学C mod_rewrite) to force the browser to always access a site with the www. prefix (adding it automatically where necessary?)

Is there an easy way (preferably with htaccess and开发者_如何学C mod_rewrite) to force the browser to always access a site with the www. prefix (adding it automatically where necessary?)

Thx.


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]


This is what I use:

$url= $_SERVER["SERVER_NAME"];
$page=$_SERVER["REQUEST_URI"]; 
if($url == "example.com"){
    header("Location: http://www.example.com$page");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号