Infinite loop with VirtualDocumentRoot, .htaccess and mod_rewrite

Posted on

Infinite loop with VirtualDocumentRoot, .htaccess and mod_rewrite – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, .htaccess, mod-rewrite, virtualhost, .

I want to set up robust multiuser development environment with apache2.

So I placeed the following file to etc/apache2/sites-available/mydomain.com

<virtualhost *:80>
    ServerName  mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /var/www/mydomain.com/prod
</virtualhost>

<virtualhost *:80>
    VirtualDocumentRoot /var/www/mydomain.com/%1
    ServerName dev.mydomain.com
    ServerAlias *.dev.mydomain.com
</virtualhost>

And the following .htaccess file is copied to each /var/www/mydomain.com/* directory:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L,NS]

Finaly it works fine for requests like http://mydomain.com/, but not for http://mydomain.com/foo.

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.
[debug] core.c(3112): r->uri = /var/www/mydomain.com/foo/index.php
…
[debug] core.c(3118): redirected from r->uri = /var/www/mydomain.com/foo/index.php
[debug] core.c(3118): redirected from r->uri = /foo

So how can I fix that and what is wrong?

Solution :

A common misconception is that rewriterules belong in .htaccess files. They don’t. Actually putting rewriterules there is a bad idea.

You have access to your server config. You should put your rules there (if you choose touse any…)

However, there is a better solution for the “redirect everything not found to index.php”. And this is to use fallbackresource:

http://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource

<virtualhost *:80>
    VirtualDocumentRoot /var/www/mydomain.com/%1
    ServerName dev.mydomain.com
    ServerAlias *.dev.mydomain.com
    FallBackResource /index.php
</virtualhost>

Leave a Reply

Your email address will not be published.