i saw .htaccess Redirect non-WWW to WWW preserving URI string but it doesn't work for me
if i go to mysite.com/site/something i get redirected to mysite.com/something
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
also tried:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.mysite.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
edit:
here's the code im using, based on Alfonso Rubalcava's answer:
if (substr($_SERVER['SERVER_NAME'], 0, 3) != 'www')
{
if ($_SERVER['REQUEST_URI'] == '//site/')
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.site.com/site/');
exit;
}
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.site.com' . $_SERVER['REQUEST_URI']);
exit;
}