mod_rewrite loops even with L flag

为君一笑 提交于 2019-12-18 08:14:24

问题


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?


回答1:


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.




回答2:


Hy , add this after RewriteEngine On

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

.. and it should work stoping loops .




回答3:


Apparently -- and I only read this here, I have no first hand knowledge -- the [L] directive does not work in .htaccess files, only if its in your .conf file.

See: Hidden features of mod_rewrite

within the .htaccess context, [L] will not force mod_rewrite to stop. it will continue to trigger internal




回答4:


Faced the same problem, and it turns out that the best solution in Apache 2.3.9+ is to use END flag instead of L as it prevents mod_rewrite from looping over rules.



来源:https://stackoverflow.com/questions/2127131/mod-rewrite-loops-even-with-l-flag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!