How to redirect sub.example.com to www.example.com with .htaccess?

0

Issue

I would like to redirect sub.example.com to www.example.com. I used the Rewrite of .htaccess. I wrote this:

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

I already activate the rewrite with: sudo a2enmod rewrite.

And I already rewrite example.com to www.example.com with:

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

Do you know how I have to write my Rewrite rules in this case?

Solution

If you specify the domain name, you can use one rule for both cases:

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

Answered By – Croises

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More