Infinite loop with VirtualDocumentRoot, .htaccess and mod_rewrite

.htaccessapache-2.2mod-rewritevirtualhost

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?

Best Answer

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>
Related Topic