Reputation: 1346
I have some unused subdomains and I would like to redirect them to my homepage.
I have found this .htaccess code
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/ [L,R]
It redirects when you call the exact subdomain, for example: test.example.com
But it does not work if you call anything else, for example: test.example.com/meh
It's possibile to redirect any URL in these subdmoains to the homepage?
Upvotes: 1
Views: 44
Reputation: 786021
You may use this rule:
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
%{REQUEST_URI}
at the end of target will add previous URI to new target.
Upvotes: 1