Tutorial :mod_rewrite help to pass full path with query string



Question:

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]  


Solution:1

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.


Solution:2

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]  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »