Forum Moderators: phranque

Message Too Old, No Replies

multi-variable mod rewrite

multi-variable mod_rewrite

         

MunchSoft

2:27 am on Nov 3, 2007 (gmt 0)

10+ Year Member



First post :) This is a great forum. I just have a quick question about mod_rewrite. I'm trying to create an .htaccess rewrite rule that does the following:

Install.html?rid=123

is turned into this:

index.php?page=Install&rid=123

Here's the rule I tried to write:

RewriteEngine on
Rewriterule ^(.*).html?(.*)$ index.php?page=$1&$2
Rewriterule ^(.*).html$ index.php?page=$1

It works, except in order to get my second variable passed to $_GET, I have to do: Install.html&rid=123

... using the '&' instead of '?' to prepend the query string. Did I miss something in this?

Thanks and have a great evening!

- James

[edited by: MunchSoft at 3:17 am (utc) on Nov. 3, 2007]

jdMorgan

4:28 am on Nov 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One rule using the [QSA] flag (Query String Append) will replace both of your rules:

RewriteEngine on
RewriteRule ^([^.]+\.html$ index.php?page=$1 [QSA,L]

I changed the regular expressions pattern to make it more efficient and also to escape the period and declare it as a literal character rather than its regex meaning of "match any single character."

Jim

MunchSoft

4:46 am on Nov 3, 2007 (gmt 0)

10+ Year Member



I get a 500 Internal Server Error when using that script. Is there only supposed to be one '(' at the beginning of the rule?

Thanks!

MunchSoft

4:48 am on Nov 3, 2007 (gmt 0)

10+ Year Member



Apache/2.0.52 (Red Hat) btw

MunchSoft

4:57 am on Nov 3, 2007 (gmt 0)

10+ Year Member



Error says: RewriteRule: cannot compile regular expression '^([^.]+\\.html$'

[edited by: MunchSoft at 5:00 am (utc) on Nov. 3, 2007]

MunchSoft

5:10 am on Nov 3, 2007 (gmt 0)

10+ Year Member



Used


RewriteEngine on
Rewriterule ^(.*).html?(.*)$ index.php?page=$1&$2 [QSA,L]

Works ;)

jdMorgan

2:12 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, missing right parenthese:

RewriteEngine on
RewriteRule ^([^.]+)\.html$ index.php?page=$1 [QSA,L]

The query string is not part of the URL 'seen' by rewriterule, so in your code, $2 will always return blank. It is the [QSA] flag that is passing the original query through the rule, not the $2 back-reference.

Jim