mod_rewrite的htaccess的:以prevent循环最好的方法? [英] mod_rewrite in htaccess: best way to prevent looping?
问题描述
我有一个.htaccess文件在我的htdocs / MVC /文件夹看起来像这样
I have a .htaccess file in my htdocs/MVC/ folder which looks like this
RewriteEngine On
# if its already been rewritten to the specific folder
RewriteCond %{REQUEST_URI} ^/MVC/public/img/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/MVC/public/index.php$
# skip next two rules
RewriteRule .? - [S=2]
# rewrite image requests
RewriteRule (.*\.(jpg|png|gif))$ public/img/$1 [L]
# rewrite all other requests to the front controller index.php
RewriteRule (.*) public/index.php?url=$1 [QSA,L]
我想实现的是将文件夹MVC /公/ IMG所有其他请求将文件夹中的前端控制器index.php的MVC /公共所有的图像请求。这是建立这些规则和prevent mod_rewrite的循环最好的方法是什么?
What I want to achieve is all image requests going to the folder MVC/public/img all other requests going to the front controller index.php in the folder MVC/public. Is this the best way to set up those rules and prevent mod_rewrite to loop?
推荐答案
使用取值
标志跳过是不错,但不足之处是:
Using the S
flag to skip is fine but the downside is:
- 这是一个有点难以阅读(如果别人在看着吧)
- 如果您需要添加新的规则,你需要检查它跳过方式
- 如果您的规则变得越来越复杂,它变得越来越难保持正确的跳绳
其他可能的方式:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
在你的规则最顶端把这个会立即完全停止重写,如果在previous环应用于一个规则(或涂覆在mod_alias中重定向)。
Putting this at the very top of your rules will instantly stop rewriting altogether if a rule was applied (or a mod_alias redirect was applied) in the previous loop.
所以它会是这样的:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# rewrite image requests
RewriteRule (.*\.(jpg|png|gif))$ public/img/$1 [L]
# rewrite all other requests to the front controller index.php
RewriteRule (.*) public/index.php?url=$1 [QSA,L]
优点:你永远不用担心循环,因为第二次瞬间周围的ENV检查结束
缺点:如果你碰巧需要创建一个循环的规则,这种方法将prevent从以往的工作
Pros: You never have to worry about looping, because the 2nd time around instantly ends with the ENV check Cons: If you ever happen to need to create a rule that loops, this method will prevent it from ever working
这篇关于mod_rewrite的htaccess的:以prevent循环最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!