If I request the page http://www.mysite.com/frogs/ 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?



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.

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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