Martin Heralecký
Martin Heralecký

Reputation: 5779

Redirecting subdomains to their corresponding folders

Is it possible to configure .htaccess so any subdomain will be redirected to it's corresponding folder without need to edit .htaccess file every time I want to add the subdomain? How?

foo.domain.com -> /subdomains/foo
bar.domain.com -> /subdomains/bar

anything.domain.com/file.txt -> /subdomains/anything/file.txt

Thanks!

Upvotes: 1

Views: 44

Answers (1)

anubhava
anubhava

Reputation: 784898

You can use this rule in site root .htaccess:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteRule ^((?!subdomains/).*)$ subdomains/%1/$1 [L,NC]

Upvotes: 1

Related Questions