Gryz Oshea
Gryz Oshea

Reputation: 361

redirect with www

RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

If I open mysite.com then this redirects me on www.mysite.com. This is OK. But if I open mysite.com/admin then this redirect me on www.mysite.com instead of www.mysite.com/admin

How can I fix it?

Upvotes: 1

Views: 304

Answers (1)

Ben Swinburne
Ben Swinburne

Reputation: 26497

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

Should do the trick. Use the parentheses () to match a pattern then a $1 backreference to put it back into the new URL.

Edit, thinking about it, similarly you could just change the ^ character (the match on clause) to a . in your original pattern which should do the same as above.

RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule . http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 1

Related Questions