Jacob
Jacob

Reputation: 66

mod_rewrite: Redirect to cache or script

I'm having some troubles with my mod_rewrite configuration, for a "cache" solution that I'm working on. I have directory called "cache" with compiled html files. I also have an index.php file which handles all the requests.

Right now all requests are rewritten to the index.php file like: "/articles" => index.php?q=articles.

But now I want it to rewrite to "cache/articles/index.html" if it exists, or else just fall back and rewrite to the index.php script.

I have messed around with the rewrite for a while know, but I can't get it to work. The rewrite code looks like this now (without anything for the cache):

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

</IfModule>

Thanks in advance :)

Upvotes: 0

Views: 946

Answers (1)

Ulrich Palha
Ulrich Palha

Reputation: 9509

Try adding the following to the .htaccess file in the root directory of your site.

RewriteEngine on
RewriteBase / 

#if the index.html exists in the cache
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}/index.html -f [NC] 
#then display it
RewriteRule ^ cache%{REQUEST_URI}/index.html [L]

#other existing rules go here

Upvotes: 1

Related Questions