Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

apache - mod_rewrite loops even with L flag

I've got a problem with rewriting a URL to a fastcgi dispatcher. If I leave only:

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

I expected L (last rule) to cause only a single rewrite. Instead, it keeps prepending dispatch.fcgi until apache reports an error.

I know 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 something else than I think it does?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know it's an old question, but to others searching for the REAL answer, here it is:

The [L] flag DOES work in .htaccess files. It tells the rewrite module to skip all of the following rules in that 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 whole url matching 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 option (10 by default) stops it.

The !-f file attribute test (as mentioned by the questioner) would solve the problem, since the url will match a real filename:

RewriteCond %{REQUEST_FILENAME} !-f

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

now, if we request http://example.com/toappend, .htaccess rewrites it to dispatch.fcgi/toappend and no rewrite loop will happen.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...