2

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
  • Unfortunately, I do not have access to these logs. My web-hosting provider charges an extra fee for it. Commented Sep 16, 2020 at 20:21
  • 1
    Try last line as: RewriteRule ^(?!domain/)(.*)$ domains/%{HTTP_HOST}/$1 [L]
    – anubhava
    Commented Sep 16, 2020 at 20:25

2 Answers 2

1

You can actually simplify it further by using this negative condition rule:

RewriteRule !^domains/ domains/%{HTTP_HOST}%{REQUEST_URI} [L,NC]
1

As I played with it at the localhost I found out that it starts cycling for some reason. The log says:

[core:error] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://subdomain.example.org/
[core:debug] AH00121: r->uri = /domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/favicon.ico, referer: http://subdomain.example.org/
[core:debug] AH00122: redirected from r->uri = /domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/favicon.ico, referer: http://subdomain.example.org/
...
[core:debug] AH00122: redirected from r->uri = /domains/subdomain.example.org/favicon.ico, referer: http://subdomain.example.org/
[core:debug] AH00122: redirected from r->uri = /favicon.ico, referer: http://subdomain.example.org/

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 directive RewriteEngine On into the subdomain.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.

1
  • 1
    Solution 1 - (?!domain/) - This would presumably need to be (?!domains/) (with an s)? (In order to avoid a rewrite loop, hence 500 response.) Solution 2 - "even a blank .htaccess" - a completely blank file wouldn't do anything. It would need to contain RewriteEngine On (or Off) in order to override the parent mod_rewrite directives (thus preventing a rewrite loop). Mod_rewrite is not inherited by default which is why the other two domains (with a .htaccess file presumably containing mod_rewrite directives) does not trigger a rewrite loop.
    – MrWhite
    Commented Sep 16, 2020 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.