I am trying to set up mod rewrite to forward the site example.com to www.example.com. This is fairly easy, because there is a line in the built in .htaccess file that I just have to uncomment. The problem is, I can't figure out how to set it so that all domains of example.com forward to www.example.com, accept m.example.com which I want to keep the same. These are the line's I'm using right now:

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

How can I set it to so that only m.example.com stays the same?

1 Answer 1


Something like this should do it:

RewriteCond %{HTTP_HOST} !^(www|m)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.