开发者

RewriteCond capture group not matching

开发者 https://www.devze.com 2023-02-03 07:43 出处:网络
I am setting up Zend_Cache_Backend_Static and as per the manual ( http://framework.zend.com/manual/en/zend.cache.backends.html ), it requires \"some changes to the default .htaccess file in order for

I am setting up Zend_Cache_Backend_Static and as per the manual ( http://framework.zend.com/manual/en/zend.cache.backends.html ), it requires "some changes to the default .htaccess file in order for requests to be directed to the static files if they exist".

My problem is that the suggested pattern below does not work for me:

RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST开发者_如何学运维_URI}.(html|xml|json|opml|svg) -f
RewriteRule .* cached/%{REQUEST_URI}.%1 [L]

I broke the rule down and found that the following works for me:

RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}.html -f
RewriteRule .* cached/%{REQUEST_URI}.html [L]

Breaking it down further the following pattern does not work:

RewriteCond %{DOCUMENT_ROOT}/cached/8hc.(html|xml) -f
RewriteRule (.*) cached/8hc.html [L]

and this does:

RewriteCond %{DOCUMENT_ROOT}/cached/8hc.html -f
RewriteRule (.*) cached/8hc.html [L]

It seems to me like the capture-group brackets in the RewriteCond are not working.

Can anyone explain why this could be? Have I not set some setting in Apache?

Apache version Apache/2.2.14 (Win32)


The syntax of RewriteCond is different from RewriteRule:

RewriteCond TestString CondPattern

Here CondPattern is the part that allows a regular expression or other special patterns like -f. But TestString is a plain string where only environment variables (%{NAME}) and references ($n, %n) are expanded.

So in order to have this work, you need some kind of case differentiation like this:

RewriteCond .html .+
RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}%0 -f [OR]
RewriteCond .xml .+
RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}%0 -f [OR]
RewriteCond .json .+
RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}%0 -f [OR]
RewriteCond .opml .+
RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}%0 -f [OR]
RewriteCond .svg .+
RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}%0 -f
RewriteRule .+ cached/%{REQUEST_URI}%0 [L]

Here each test consists of two conditions where the first condition is used to catch the file name extension to be used in the RewriteRule.


Here is a site that I use to test my rewrite rules: Rewrite Tester

0

精彩评论

暂无评论...
验证码 换一张
取 消