# Apache -- using mod_rewrite and mod_cache together

## bradp_84

I am wanting to use mod_disk_cache to cache dynamic content. Basically, I have a .htaccess file that looks something like this:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*) index.php?url=$1&%{QUERY_STRING} [L]

Which says, if the file requested does not exist, rewrite the request to the index.php file and pass the requested url to the url variable in the GET string. The end result is that a request to /privacy gets rewritten to index.php?url=privacy which then looks up the page in a database and prints it out.

Turning on mod_disk_cache and setting the server log level to debug, I get the following in error_log when do a wget on /privacy:

[Sat Aug 02 11:04:09 2008] [debug] mod_rewrite.c(1789): [client 192.168.12.8] mod_rewrite's internal redirect status: 0/10.

But if I access index.php?url=privacy using wget, I get this in the error_log:

[Sat Aug 02 11:07:31 2008] [debug] mod_cache.c(556): cache: Caching url: /index.php?url=privacy

[Sat Aug 02 11:07:31 2008] [debug] mod_cache.c(598): cache: Added date header

[Sat Aug 02 11:07:31 2008] [debug] mod_disk_cache.c(642): disk_cache: Stored headers for URL 192.168.12.8/index.php?url=privacy

[Sat Aug 02 11:07:31 2008] [debug] mod_disk_cache.c(744): disk_cache: Body for URL 192.168.12.8/index.php?url=privacy cached.

Is there a trick to getting mod_cache to cache pages when mod_rewrite is used or is this not possible?

I am using apache 2.0.55, but I have tried it on apache 2.2.9 as well without success.

Thanks in advance.

Cheers,

Brad

----------

## kimmie

You could try moving the LoadModule statement for mod_cache below that for mod_rewrite in /etc/apache2/httpd.conf. I had to fiddle with the order of mod_rewrite and mod_proxy once in a similar situation, and that fixed it. According to some doco I've seen the hooks are called in reverse order of loading, so existing order should be ok. Still, might be worth trying.... 

But first debug your rewrite rule... use RewriteLog / RewriteLogLevel. I think you may have two problems, the location of the / and trying to substitute the existing query string along with new terms. You could try this rule instead:

RewriteRule ^/(.*) /index.php?url=$1 [L,QSA]

----------

