Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite - add www

I'm trying to force www for my site address with .htaccess:

RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.coml/$1 [R=301,L]

If I open mysite.com this working ok - it adds www. It becomes http://www.mysite.com/index.php.

But if I open mysite.com/subpage I redirected to http://www.mysite.com/index.php again, instead of http://www.mysite.com/subpage.

But if I open http://www.mysite.com/subpage I don't get redirect (which is the expected behaviour).

How can I fix this? I would like the redirect all requests to my site to the www subdomain.

like image 408
Nick Huge Avatar asked Sep 21 '11 00:09

Nick Huge


1 Answers

From the superb HTML5 Boilerplate .htaccess:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
like image 182
Alex Avatar answered Sep 21 '22 17:09

Alex