Redirect non existing subdomains

From snippet wiki
Jump to navigation Jump to search

Having a single installation with multiple domains and subdomains, you can redirect non existing subdomains to the main page. Otherwise you might get either duplicate content or unwanted subdomain names in google like neverbuy.exaple.com which doesn't seem very professional.

RewriteEngine On

RewriteCond %{HTTP_HOST} !^landing.example.com
RewriteRule (.*) http://www.example.com/ [R=301,L]

If you have multiple domains under one virtual server you might want to add a condition for checking the top level domain:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^landing.example.com
RewriteCond %{HTTP_HOST} example.com
RewriteRule (.*) http://www.example.com/ [R=301,L]

RewriteCond %{HTTP_HOST} !^landing.example.com
RewriteCond %{HTTP_HOST} example.de
RewriteRule (.*) http://www.example.de/ [R=301,L]