skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


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

    RewriteRule !^domains/ domains/%{HTTP_HOST}%{REQUEST_URI} [L,NC]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search