RewriteRule and "?" in filename issue

#1
Hello,

I can't make some RewriteRule.

This is a example from Zend Framework that works on Apache 2.2 and 2.4 - On LiteSpeed doesn't

Code:
RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/cache/index.html -f
RewriteRule ^/*$ cache/index.html [L]

RewriteCond %{DOCUMENT_ROOT}/cache/%{REQUEST_URI}.(html|xml|json|opml|svg) -f
RewriteRule .* cache/%{REQUEST_URI}.%1 [L]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ index.php [NC,L]
So after few hour i make this:

Code:
RewriteEngine On

RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{DOCUMENT_ROOT}/cache/index.html -f
RewriteRule ^/*$ cache/index.html [L]

RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{DOCUMENT_ROOT}/cache/%{CURRENT_URI}?%{QUERY_STRING}\.html -f
RewriteRule ^.*$ %{DOCUMENT_ROOT}/cache/%{CURRENT_URI}?%{QUERY_STRING}\.html [L]

RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{DOCUMENT_ROOT}/cache/%{CURRENT_URI}\.html -f
RewriteRule ^.*$ cache/%{CURRENT_URI}\.html [L]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ index.php [NC,L]

But this doesn't work:

Code:
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{DOCUMENT_ROOT}/cache/%{CURRENT_URI}?%{QUERY_STRING}\.html -f
RewriteRule ^.*$ %{DOCUMENT_ROOT}/cache/%{CURRENT_URI}?%{QUERY_STRING}\.html [L]
This line check that file exist successfully (/cache/example.html?xxx=111.html)
Code:
RewriteCond %{DOCUMENT_ROOT}/cache/%{CURRENT_URI}?%{QUERY_STRING}\.html -f
This one make redirect not to file, but goes forward: (there is nothing notice about file not exist in LOG file)
Code:
RewriteRule ^.*$ %{DOCUMENT_ROOT}/cache/%{CURRENT_URI}?%{QUERY_STRING}\.html [L]
But if I change "?" for any other char like , "-", "_" etc. everything works fine.

Summary
If i execute: www.example.com/examle.html?xxx=zzz should be Rewrited to file /cache/examle.html?xxx=zzz.html -> this doesnt work

But if i make rule to: www.example.com/examle.html?xxx=zzz should be Rewrited to file /cache/examle.html_xxx=zzz.html -> this is works

This cond return true:
Code:
RewriteCond %{DOCUMENT_ROOT}/cache/%{CURRENT_URI}?%{QUERY_STRING}\.html -f
But this rule not redirect to file:
Code:
RewriteRule ^.*$ %{DOCUMENT_ROOT}/cache/%{CURRENT_URI}?%{QUERY_STRING}\.html [L]


Is there anybody can help me?
 
Last edited:

NiteWave

Administrator
#2
question mark("?") has special meaning in rewriterule
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
not sure use \? instead of ? will work.

also '/cached/' in first rewriterule example and '/cache/' in rest examples. assume it's in purpose and not typo.
 

mistwang

LiteSpeed Staff
#4
You should avoid using "?" in the rewrite target if it is not intended for query string part, it will definitely break Apache rewrite engine as well. According to the spec.
If you have to, you can try URL encode it with "%3f".
 
Top