Tantalus
Tantalus

Reputation: 457

How do I redirect all non-existent subdomain urls to my main url?

On my Drupal site the provided .htaccess file allows me to redirect www.example.com requests to example.com using this code:

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

In addition to this, I would also like to point all subdomain requests to example.com as well. For example:

So, how would I modify the above code to accomplish this?

Upvotes: 1

Views: 364

Answers (1)

anubhava
anubhava

Reputation: 785491

You can have another rule after your current rule:

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]

Upvotes: 1

Related Questions