views:

593

answers:

2

I'm doing some caching based on the whole url, including query string, and need Apache to recognize the file and use it. It seems to find a match but then strips the query string off before rendering the file.

url: www.somesite.com/default.asp?foo=bar

Cached page filename on the filesystem in the cache folder: default.asp?foo=bar

My current attempt at rewrite:

RewriteCond /site_root/cache/%{REQUEST_FILENAME}?%{QUERY_STRING} -f
RewriteRule ^.*$ /site_root/cache/%{REQUEST_FILENAME}?%{QUERY_STRING} [L,QSA]
+1  A: 

You might want to try something like this:

RewriteRule ^(.+)$ /site_root/cache/$1 [L,QSA]

This should rewrite the "default.asp" in the URL to what I think you're trying to use. The QSA should cause the original querystring to be appended to the rewritten URL.

I'm not 100% sure this is what you're looking for, so if it's not let me know and I will try to help.

bchang
Thanks superchink. I'm using RewriteLog to watch what's happening and it looks like your attempt drops the query string and only tries to match on default.asp.I need the filename and query string to be preserved so that apache can serve the file with the same name. It doesn't matter if it's default.asp or index.php or foobar.baz, it needs to serve the file that matches. The filename includes the query string as I mentioned abvoe.
A: 

I found a solution that works for my need.

It seems that Apache really wants to pull out the query string into a set of params, which is normal.

In order to pull one over on Apache I just replaced the ? in the string with a |. Once I did that it didn't see the string as a query string anymore. Since the params are irrelevant in my case it works fine. All I have to do is make sure I write out the filename with a pipe instead of a question mark. Easy enough.

Feel free to comment if you have a better solution.

Here's the rule that's doing it for me. Since Apache doesn't recognize the result as a MIME it knows, I force it to text/html.

RewriteRule ^(.+)$ /site_root/cache/%{HTTP_HOST}$1|%{QUERY_STRING} [L,T=text/html]