PDA

View Full Version : RewriteRule Errors with ?


Steven Edwards
01-31-2008, 12:43 PM
Hi guys,

I'm helping a friend set up RewriteRule directives for a site and LiteSpeed doesn't seem to like them. I'm redirecting everything to Google to make sure it's not a box error, then going back to his site.

The following directives work:

---
RewriteEngine On
RewriteRule ^([0-9]+) http://www.google.com/$1 [R=301,L]

//

RewriteEngine On
RewriteRule ^test([0-9]+) http://www.google.com/$1 [R=301,L]

//

RewriteEngine On
RewriteRule ^test/([0-9]+) http://www.google.com/$1 [R=301,L]
---

But the following fails:

---
RewriteEngine On
RewriteRule ^test?([0-9]+) http://www.google.com/$1 [R=301,L]

//

RewriteEngine On
RewriteRule ^test\?([0-9]+) http://www.google.com/$1 [R=301,L]
---

As soon as I add the ? character, LiteSpeed gives me a 404.

I checked prior threads on Google (site:litespeedtech.com rewriterule), but found none that specifically mentioned this.

Suggestions?

Thanks,

Steven

mistwang
01-31-2008, 12:54 PM
first, "?" is a special character in Regular expression, so you need to escape it with a "\".
Second, RewriteRule only match the URI part of the URL, query string part will not be matched. You need to add a "RewriteCond ${QUERY_STRING} ..."

Steven Edwards
01-31-2008, 01:00 PM
first, "?" is a special character in Regular expression, so you need to escape it with a "\".
Second, RewriteRule only match the URI part of the URL, query string part will not be matched. You need to add a "RewriteCond ${QUERY_STRING} ..." Hi Mistwang,

I forgot to include that I tried escaping the character at first; I was editing my post as you replied to indicate that.

Thanks for the quick reply. The same directive works perfectly on an Apache server with another hosting company. I'll lookup the RewriteCond directive in the documentation. Would prepending "RewriteCond ${QUERY_STRING}" to the existing directives be enough to fix it?

mistwang
01-31-2008, 01:03 PM
RewriteEngine On
RewriteCond ${QUERY_STRING} ([0-9]+)
RewriteRule ^test$ http://www.google.com/%1 [R=301,L]

Steven Edwards
01-31-2008, 01:04 PM
RewriteEngine On
RewriteCond ${QUERY_STRING} ([0-9]+)
RewriteRule ^test$ http://www.google.com/%1 [R=301,L] Thanks a ton. :)