zee
zee

Reputation: 359

.htaccess Redirect all subdomains to corresponding folder

I'm using this .htaccess to redirect each subdomain to a folder named the same as subdomain and all requests to corresponding index.php.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.com$
RewriteCond %{REQUEST_URI} !^/sub/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub/$1
RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.com$
RewriteRule ^(/)?$ sub/index.php [L]

It works great, but I'd like it to work with multiple subdomains.

Based on several answers I found in SO, the code should look somewhat like the below, but I can't get it to work.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?(.*).domain.com$
RewriteCond %{REQUEST_URI} !^/(.*)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteCond %{HTTP_HOST} ^(www.)?(.*).domain.com$
RewriteRule ^(/)?$ %1/index.php [L]

Upvotes: 0

Views: 625

Answers (2)

arkascha
arkascha

Reputation: 42984

This should be the straight forward approach:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [QSA,END]

If will redirect all host names within the domain "example.com" and drop that historic "www." prefix people are always so worried about. This does not handle the main domain (with or without "www." prefix), you'd have to add that if you do not operate a separate virtual host for that special one. The index.php stuff should better be handled using the DirectoryIndex directive the apache http server offers. This leads to less complex setups and cleaner URLs.

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup, certainly you will need to add another rewriting condition to break an endless rewriting loop.

This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Upvotes: 0

Kate
Kate

Reputation: 1836

Something like this should do wildcard redirect, unless the domain name begins with www. Note the negative condition using the exclamation mark.

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^ http://example.com/%1/index.php [R=301,L]

^(.+?) captures whatever precedes domain.com, unless it is www.

PS: sorry for the last line, it should read domain.com (whatever your actual domain is) instead of example.com. I had to to do this because of posting restrictions on SO.

Upvotes: 1

Related Questions