Deepak
Deepak

Reputation: 13

Redirect non WWW urls to WWW urls through htaccess

I am using apache 2.2 on a windows OS , I need to redirect non www urls to www urls , i checked online , and found this seems to be working for many

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

But if i use the same rewrite rules i get www urls with a dot in the end :

ex :

http://example.com/test redirects to
http://www.example.com.
$1 is "." and its failing to add all params

Upvotes: 0

Views: 397

Answers (1)

undone
undone

Reputation: 7888

Ok!
you put this on you .htaccess file:

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

it means if host equals to mysite.local (end of host not mentioned!) so every url redirects to directory mysite.local!
You should use this:

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

So every URL that host IS NOT equal to www.mysite.local will redirect to this one! but remember if you want to create subdomain, PUT DOCUMENT ROOT OF THIS SUBDOMAIN OUT OF WWW.MYSITE.LOCAL DOCUMENT ROOT! (it's better!)

Upvotes: 1

Related Questions