我有以下htaccess重写规则.防止循环的一个规则条件最初是这样编写的:
RewriteCond %{ENV:REDIRECT_STATUS} ^.
Run Code Online (Sandbox Code Playgroud)
它曾经工作得很好,直到它突然停止工作,导致Apache显示网站的目录列表.
我必须将它更改为这个新表单,如下面的清单,让它再次工作:
RewriteCond %{ENV:REDIRECT_STATUS} 200
Run Code Online (Sandbox Code Playgroud)
你知道这种行为的原因吗?
谢谢
RewriteEngine on
RewriteBase /
## Permanent 301
## Force to www. Un-comment in production.
RewriteCond %{HTTP_HOST} !^www\.myhost\.com [NC]
RewriteRule ^(.*) http://www.myhost.com/$1 [L,R=301]
## Permanent redirect rules for contents
RewriteRule ^argument/programming/?$ tags/programming [NC,L,R=301]
## Internal Redirect Loop Protection
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
## Maintenance page
#RewriteRule (.*) special/maintenance.html
## Specials
RewriteRule special/(.*) special/$1 [NC,L]
## Static resources
RewriteRule ^(.*\.(js|ico|gif|jpg|png|css|rss|xml|htm|html|pdf|zip|gz|txt))$ public/$1 [NC,L]
## Front Controller
RewriteRule ^(.*) public/index.php …Run Code Online (Sandbox Code Playgroud) 我需要一个正则表达式,它只会选择那些不以 .png 或 .css 等特定扩展名结尾的 URL 字符串。
我测试了以下内容:
1)这个使用负回顾:
(?<!\.png|\.css)$
Run Code Online (Sandbox Code Playgroud)
https://regex101.com/r/tW4fO5/1
2)另一个使用负前瞻:
^(?!.*[.]png|.*[.]css$).*$
Run Code Online (Sandbox Code Playgroud)
https://regex101.com/r/qZ7vA4/1
两者似乎都工作正常,但据说 #1(负向后视)在 436 步(见链接)中处理,而 #2(负向后视)据说在 173 步中处理。
所以我的问题是:这是什么意思?会不会影响演出?
最后,这两个正则表达式在功能上真的是等价的吗?
编辑:解决方案摘要
总结一下,考虑到要通过正则表达式排除的字符串结尾的完整列表(一个典型的场景是 Web 服务器设置,其中静态资源由 apache 提供,而动态资源由不同的引擎提供 - 在我的情况下: php-fpm)。
PCRE 正则表达式有两种可能的选择:
1)负面回顾
$(?<!\.(?:ico|gif|jpg|png|css|rss|xml|htm|pdf|zip|txt|ttf)$|(?:js|gz)$|(?:html|woff)$)
https://regex101.com/r/eU9fI6/1
请注意,我使用了几个 OR ed 后视,因为负后视需要固定宽度的模式(即:您不能混合不同长度的模式)。这使得这个选项的编写稍微复杂一些。此外,在我看来,这降低了它的性能。
2)负前瞻
^(?!.*[.](?:js|ico|gif|jpg|png|css|rss|xml|htm|html|pdf|zip|gz|txt|ttf|woff)$).*$
https://regex101.com/r/dP7uD9/1
前瞻比后视略快。这是 100 万次迭代的测试结果:
后视时间 = 18.469825983047 秒
前瞻时间 = 14.316685199738 秒
如果我没有可变长度模式的问题,我会选择后视,因为它看起来更紧凑。反正哪一个都好。最后,我向前看:
<LocationMatch "^(?!.*[.](?:js|ico|gif|jpg|png|css|rss|xml|htm|html|pdf|zip|gz|txt|ttf|woff)$).*$">
SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://www/srv/www/gioplet/web/public/index.php"
</LocationMatch>
Run Code Online (Sandbox Code Playgroud) regex negative-lookbehind negative-lookahead regex-lookarounds