Forum Moderators: phranque

Message Too Old, No Replies

Special escaping of quantifiers in mod_rewrite?

I want to use the question mark as a regular character ...

         

phoenix_fly

12:08 am on May 12, 2005 (gmt 0)

10+ Year Member



Hello,

I have this regexp:

^([^/]+)/(.+)

But I want to track a question mark, instead of a slash:

^([^?]+)\?(.+)

Because the url is something like:

[mysite.com...]

and not:

[mysite.com...]

(what I can accomplish with javascript, but would be unnecessary brute force if I manage mod_rewrite to see the "?" as a regular character)

The problem is that the backslash doesn´t seem to work, I don´t understand why. Is there a special way to escape the "?" in mod_rewrite? Maybe use the hex equivalent, or sth?

Thanks

André

jd01

12:24 am on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi André,

I think the problem is mod_rewrite (Apache) will not see the ? (or query string) in the RewriteRule. The query string will only be parsed in a condition.

RewriteCond %{QUERY_STRING} ^regex here$
RewriteRule . /Redirect-Action [R=301,L]

Without a specific example, I cannot give you a specific solution.

Hope this gives you some direction.

Justin

phoenix_fly

2:02 pm on May 13, 2005 (gmt 0)

10+ Year Member



Hey Justin!

Thanks a lot for the help, man.

Now I managed him to see the query string, but can´t make him remember what would be %2.

Here´s the code:

RewriteEngine on
RewriteCond %{REQUEST_URI}!\.(cgi¦css¦gif¦jpg)$
RewriteCond %{HTTP_HOST} ^www\.([^\.]+)\.mysite\.com
RewriteCond %{QUERY_STRING} ^(.+)
RewriteRule ^(.+) /home/mysite/www/scripts/page.cgi?vendor=%1\&section=$1\&%2 [L]

He returns the last () in the query string cond in %1, instead of in %2, as I expected. And %2 doesn´t get nothing. How to overcome this, Justin?

I tried to put the #2 and #3 conditions together, but & and && didn´t worked.

Thanks

André

jdMorgan

3:11 pm on May 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



%n refers only to the parenthesized subexpressions in the last-matched RewriteCond, as descibed in the mod_rewrite documentation. Therefore, the trick is to get all of the () you need into one RewriteCond. One way to do this is:

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(cgi¦css¦gif¦jpg)$
RewriteCond %{QUERY_STRING}<->%{HTTP_HOST} ^([^<]+)<->(www\.)?([^\.]+)\.mysite\.com
RewriteRule (.+) /home/mysite/www/scripts/page.cgi?vendor=%3&section=$1&%1 [L]

Note that the "<->" character string is completely arbitrary, and means nothing to mod_rewrite. It is used only to create an unambiguous delimiter between the two different variables.

Jim

phoenix_fly

4:48 pm on May 13, 2005 (gmt 0)

10+ Year Member



Hello Jim!

Thanks for the clever solution! In fact, very creative that one!

It´s working just perfect now! Thanks a lot!

Cheers

André