Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I request the page and use these rewrite rules:

# See if the requested URL is in our cache. If so, serve it
RewriteCond /var/www/cache%{REQUEST_FILENAME}.html -f
RewriteRule (.*) /var/www/cache/$1.html [L,QSA]

Apache tries to find a file called "frogs/.html", which of course fails. I want to use %{REQUEST_FILENAME} without the trailing slash, so that it instead looks for and serves "frogs.html".

How can I make it do that?

share|improve this question
up vote 2 down vote accepted


Test this. It should strip out a trailing slash:

RewriteCond /var/www/cache/$1.html -f
RewriteRule ^/(.+)/*$ /var/www/cache/$1.html [L, QSA]

The reason is that you can backreference the regex in parenthesis in a RewriteRule from the RewriteCond. See mod_rewrite docs for more info on the backreferencing flow.

share|improve this answer
How does that affect the RewriteCond? – Nick Mar 17 '11 at 16:32
The rewrite condition is just checking to see if the request filename exists in the cache, correct? It shouldn't affect that. The first line I have will rewrite to add .html if there is no slash, and the second will rewrite without the slash if one is present. I'm sure theres a way to do it in one line, but I'm not that gifted in RegEx. – Hyppy Mar 17 '11 at 16:38
Ahh, I see what you're saying. I misread your question a bit. Gimme a minute... – Hyppy Mar 17 '11 at 16:40
@Nick Take a look at the edit. – Hyppy Mar 17 '11 at 16:59
Thanks, It seems to be working! I changed it to "RewriteRule ^/(.*)/$" since "RewriteRule ^/(.+)/*$" wasn't working? – Nick Mar 18 '11 at 18:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.