htaccess redirect all subdomains

Last updated on November 17th, 2015 at 05:40 am

Simple htaccess trick to redirect all subdomains except some to the main URL.

Here we have a website named anywebsite.com. So we dont want to redirect 2 subdomains namely demo.anywebsite.com and dev.anywebsite.com to the main URL and want every other subdomain to be redirected then please follow the below rules. Make sure you modify it according to your requirement.

Rewrite condition format
RewriteCond %{HTTP_HOST} !^SUBDOMAIN_NAME\.DOMAINNAME\.com$ [NC]

Complete rule to be added to the .htaccess file.

RewriteCond %{HTTP_HOST} !^demo\.anywebsite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.anywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://anywebsite.com/$1 [L,R=301]

Please make sure you make a backup of the existing htaccess file before proceeding.

Click to rate this tutorial!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *