Mod_rewrite loop even with L flag

I am having trouble rewriting the url to the fastcgi dispatcher. If I leave only:

RewriteRule ^(.*)$ dispatch.fcgi/$1 [L,QSA] 

I was expecting L (the last rule) to cause only one rewrite. Instead, it continues to add dispatch.fcgi until apache reports an error.

I know that it can be fixed with:

 RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ dispatch.fcgi/$1 [L,QSA] 

But what is the reason for multiple rewrites? Does L do anything else than I think?

+6
apache mod-rewrite
source share
4 answers

I know this is an old question, but for others who are looking for a REAL answer , here it is:

The [L] DOES flag works in .htaccess . It tells the rewrite module skip all of the following rules in this particular .htaccess file. It does its job, Apache rewrites the url and exits the .htaccess file.

However, at the end of the .htaccess file , if the request URL has been rewritten, the entire URL mapping process starts again with the new URL .

This is what happens above, ^(.*)$ Will always match the current URL, it causes an infinite loop, only the maxredirect rewrite parameter (default 10) stops it.

Testing the !-f file attribute (as mentioned by the questionnaire) will solve the problem , as the URL will match the true file name:

RewriteCond% {REQUEST_FILENAME}! -f

RewriteRule ^ (. *) $ Dispatch.fcgi / $ 1 [L, QSA]

now if we request http://example.com/toappend , .htaccess overwrite it before dispatch.fcgi/toappend and the loop will not be rewritten .

+8
source share

Hy, add this after RewriteEngine On

 RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule .* - [L] 

.. and it should work with stops.

+6
source share

Apparently - and I just read it here, I do not have the first knowledge - the [L] directive does not work in .htaccess files, only if it is in your .conf file.

See: Hidden mod_rewrite functions

in the context of .htaccess, [L] do not make mod_rewrite stop. it will continue to run internal

+2
source share

Faced the same problem, and it turned out to be the best solution in Apache 2.3. 9+ is the use of the END flag instead of L, as it prevents the mod_rewrite rules from cycling.

0
source share