I have problems with choosing a directory by HTTP hostname.
I have a file structure like this:
.
+-- .htaccess
+-- domains
+-- 123456.a12.whatever.net
| +-- .htaccess
| +-- ...
+-- example.org
| +-- .htaccess
| +-- ...
+-- subdomain.example.org
+-- index.html
The .htaccess
file in the root directory looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)(.*)$
RewriteRule (.*) %{REQUEST_SCHEME}://%2/$1 [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/domains/%{HTTP_HOST} !-d
RewriteRule (.*) $1 [R=404,L]
RewriteCond %{DOCUMENT_ROOT}/domains/%{HTTP_HOST} -d
RewriteRule (.*) domains/%{HTTP_HOST}/$1 [DPI]
Weird thing is that it works for both 123456.a12.whatever.net
and example.org
but not subdomain.example.org
. When I try to access it I get 500 Internal Server Error. I am sure that it is a problem with the .htaccess
as in the subdomain.example.org
is really only the index.html
. Also, the error seems to be in the last line.
2
Answers
As I played with it at the localhost I found out that it starts cycling for some reason. The log says:
Even though I don't know why it loops, I found two different solutions.
Solution 1 — Fix the
.htaccess
I think that the most correct solution is the one @anubhava suggested. Just changing the last line to
RewriteRule !^domains/ domains/%{HTTP_HOST}%{REQUEST_URI}
is enough and I think it is actually more correct.Solution 2 — Make a
.htaccess
Strangely, another solution is to add
.htaccess
containing the directiveRewriteEngine On
into thesubdomain.example.org
. I think this is the reason why the other domains worked. I don't know the exact reason for this but it works.You can actually simplify it further by using this negative condition rule: