ipel
ipel

Reputation: 1346

redirect all subdomains with htaccess

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

Answers (1)

anubhava
anubhava

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

Related Questions