Forcing www prefix to PHP / htaccess / mod_rewrite

Is there an easy way (preferably with htaccess and mod_rewrite) to force the browser to always access the site using www. prefix (adding it automatically if necessary?)

thank.

+3
source share
2 answers
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]
+8
source

This is what I use:

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

Source: https://habr.com/ru/post/1738743/


All Articles