Monkey Code
Monkey Code

Reputation: 602

RewriteCond capture group not matching

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)

Upvotes: 0

Views: 1242

Answers (2)

H Hatfield
H Hatfield

Reputation: 856

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

Upvotes: 0

Gumbo
Gumbo

Reputation: 655795

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.

Upvotes: 1

Related Questions