Home Ask Login Register

Developers Planet

Your answer is one click away!

Martin Heralecký February 2016

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



anubhava February 2016

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]

Post Status

Asked in February 2016
Viewed 1,456 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life