-2

I'm having trouble tro redirect example.com to www.example.com and it's subdomain to www.test.example.com to test.example.com
And currently i'm using this:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.([^\.]+)\.([a-z]{2,4})$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
user3350731
  • 902
  • 1
  • 9
  • 26
  • Try this link similar question http://stackoverflow.com/questions/990392/htaccess-rewrite-to-redirect-root-url-to-subdirectory –  May 26 '14 at 10:29

1 Answers1

0

You will need one rule for main domain and another for sub domains:

RewriteEngine On

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

RewriteCond %{HTTP_HOST} ^www\.[^.]+\.[^.]+\.([a-z]{2,4})$
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,NE,L]
anubhava
  • 664,788
  • 59
  • 469
  • 547