Mod_rewrite: Error rewriting subdomain to directory through% {HTTP_HOST}

Problems with mod_rewrite

I have more problems than I thought when it comes to Apache mod_rewrite . I already posted one question about and it was found out, but I keep getting errors when I don't see any logical error in the configuration. Any help would be greatly appreciated!

.htaccess I am using the following:

 # Begin Rewrite Module for http://*.example.com/ # ============================================== <IfModule mod_rewrite.c> # Turn the rewrite engine on and set the base path. RewriteEngine On RewriteBase / # Map subdomains to their respective directories. RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC] RewriteRule ^(.*)$ public_subdomains/%1/$1 [L] </IfModule> 

This does not work due to too many internal redirects, resulting in a 500 Internal Server Error - here is the debug log for it:

 [Fri Feb 15 16:49:30.318509 2013] [core:error] [pid 2316:tid 1708] [client 127.0.0.1:9141] 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. [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3502): [client 127.0.0.1:9141] AH00121: r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/index.php [Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = / 

I found a peculiar solution for it, replacing the [L] flag for the [END] flag in the RewriteRule and it works fine by matching the request URI exactly with the corresponding file in the subdomain directory.

Unfortunately, it works too well. A call to subdomain.example.com/folder/ rewrites interable to example.com/public_subdomains/subdomain/folder/ excluding DirectoryIndex when it should be rewritten as example.com/public_subdomains/subdomain/folder/index.php (assuming index.php installed as DirectoryIndex).

If someone can help me shed light on this, I will be forever grateful!

0
source share
1 answer

Well, there is a logical mistake. Let me direct you to the wonderful control flow diagram from docs :

Control flow diagram about the workings of mod_rewrite

You see that internal redirection is also a request, so it is again handled by any and all rewrite rules. So, suppose the user requests something.example.com/x: RewriteRule macthes, RewriteCond is executed, so an internal redirect to public_subdomains / something / x is performed. A new request is something.example.com/public_subdomains/something/x: RewriteRule is consistent, RewriteCond is being executed ... And there you have an infinite loop.

The [L] flag only stops processing the current request: the request will be processed in the second pass in any case. The [END] flag stops the loop, but this is brute force, since you noticed that it has side effects, so it would be better to include a condition that stops the recursion. Something like that:

 RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC] RewriteCond %{REQUEST_URI} !^/public_subdomains/%1 RewriteRule ^(.*)$ public_subdomains/%1/$1 [L] 

The added RewriteCond will check if the requested uri will already start with "public_subdomains / something".

It is not verified, so problems may arise, but this is the main idea.

+4
source

All Articles