简体   繁体   中英

Infinite loop with mod_rewrite and vhost_alias

I made myself a dynamic vhost file for all directories in /home/lukasz/websites

<VirtualHost *:80>
        ServerName vm01.dev
        ServerAlias *.vm01.dev

        SetEnv APPLICATION_ENV "lukasz"

        VirtualDocumentRoot /home/lukasz/websites/%1/public
        <directory /home/lukasz/websites/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
        </directory>
</VirtualHost>

It works well until very simple rule from mod_rewrite comes to play. If I add following code to the .htacesss

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

The apache server will be unable to respond. It will go into an infinite loop and will break out from the execution after 10 redirects. This problem is tightly related to VirtualDocumentRoot I'm using. How should I modify my config to keep it dynamic and stop it breaking with about htaccess?

I'm guessing you want to make index.php the default page, when the requested resource doesn't exist, you didn't say.

If this is the case then adding an ErrorDocument entry to your virtualHost will suffice eg

ErrorDocument 404 /index.php

If you are going to use mod_rewrite, it's far more efficient to place the rules in the Virtualhost definition than in a .htaccess file. As the httpd.conf is read once at start-up and all rule compiled, as opposed to the .htaccess file being read and all rules parsed for each request. Anyway the following should stop the loop:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]

Assuming you have an: /index.php

FYI: The Apache mod_rewrite documentation is worth a read if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM