htaccess redirect all subdomain except one

I needed a rule to redirect all subdomain in my domain except one specified subdomain, to do that I use those few lines in my .htaccess file

[cc_bash]

DirectoryIndex index.php
RewriteEngine on

RewriteCond %{HTTP_HOST} !noredirect.waltertavares.pt$ [NC]
RewriteCond %{HTTP_HOST} (.*).waltertavares.pt$ [NC]
RewriteRule ^(.*)$ http://www.waltertavares.pt/$1 [L,R=301]

[/cc_bash]

Of course you can skip redirection on a full list of subdomains by duplicating the line

[cc_bash]
RewriteCond %{HTTP_HOST} !noredirect.waltertavares.pt$ [NC]
RewriteCond %{HTTP_HOST} !no-redirect.waltertavares.pt$ [NC]
RewriteCond %{HTTP_HOST} !real-sub-domain.waltertavares.pt$ [NC]
[/cc_bash]

Leave a Reply