Dal*_*lai 2 regex apache mod-rewrite
我的htdocs / MVC /文件夹中有一个.htaccess文件,看起来像这样
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]
Run Code Online (Sandbox Code Playgroud)
我要实现的是将所有图像请求都发送到文件夹MVC / public / img,将所有其他请求都发送到文件夹MVC / public中的前端控制器index.php。这是设置这些规则并防止mod_rewrite循环的最佳方法吗?
使用S标志跳过可以,但是缺点是:
其他防止循环的方法:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Run Code Online (Sandbox Code Playgroud)
如果在上一个循环中应用了规则(或应用了mod_alias重定向),则将其放在规则的最顶部将立即完全停止重写。
因此,它看起来像这样:
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]
Run Code Online (Sandbox Code Playgroud)
优点:不必担心循环,因为第二次循环立即以ENV检查结束。缺点:如果您碰巧需要创建循环规则,则此方法将使其无法正常工作