Forum Moderators: phranque

Message Too Old, No Replies

problem with multiple RewriteRules

mod rewrite, rewriterule

         

Monstah

6:38 pm on Oct 10, 2006 (gmt 0)

10+ Year Member



I´m having a problem with an URL that passes through more than one rewrite rule. I have these rules, for example:


RewriteRule ^events/(.*)/([0-9]+)events/$1&photo=$2
RewriteRule ^events/([0-9]+)(.*)$events/index.php?id_event=$1$2

I expect events/01/61 to be rewritten to events/01&id_photo=61, which would then be rewritten to events.php?id_event=01&id_photo=61. The final result, however, is events.php?id_event=01&id_photo=61/61. When I put more variables, each with its RewriteRule, they all add repeated /(...) stuff at the end. Why is that?

[edited by: Monstah at 6:41 pm (utc) on Oct. 10, 2006]

jdMorgan

6:51 pm on Oct 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The simple answer is that query strings are not part of a URL; they are data appended to a URL, to be passed to the resource *at* that URL. Therefore, query strings (and parts of query strings) are handled separately by many Apache modules, including mod_rewrite.

Also, "events/01&id_photo=61" is an invalid URL, since "&" is only allowed in URL-paths if it is encoded.

Another angle is that if you wish to replace a query string, the subsitution must contain "?" followed by the query string to replace the current one -- Use just "?" with nothing after it to clear the current query string.

If, on the other hand, you wish to append to the current query string, then either use [QSA] or do it manually by using a RewriteCond to capture the current %{QUERY_STRING} and then back-reference it in the substitution URL.

So, there are several layers of problems here.

I suggest you "build" your new query string by starting with the "?" at the beginning, and then appending from there. That avoids the invalid unencoded "&" character problem, and will likely give you more predictable results.

Jim