Often needed htaccess rules

Here are some often needed htaccess rewrite rules.

**Redirect a domain to another domain name
**To redirect traffic from the domain www.domain1.com to the domain www.domain2.com use

RewriteEngine on RewriteCond %{HTTP_HOST} www.domain1.com [NC] RewriteRule ^(.*) http://www.domain2.com/$1 [R=301,L]

**Rewrite all domain names to one domain** If you have multiple domains pointing to your server and you want to redirect them all to the single domain name www.domain.com:

RewriteEngine on RewriteCond %{HTTP_HOST} !^www.domain.com$ RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

**Rewrite a subfolder to a (sub)domain
**When you move a folder to a domain or subdomain, e.g. www.domain.com/blog to blog.domain.com, and want to redirect all traffic use this rule:

RewriteEngine on RewriteCond %{HTTP_HOST} www.domain.com [NC] RewriteCond %{HTTP_HOST} !blog.domain.com [NC] RewriteRule ^blog(.*) http://blog.domain.com$1 [R=301,L]

The rewrite condition RewriteCond %{HTTP_HOST} !blog.domain.com [NC] is only needed if your subfolder and subdomain have the same name as you will otherwise get an error message like “the webpage has resulted in too many redirects”.

Often needed htaccess rules
Share this