Forum Moderators: phranque

Message Too Old, No Replies

Variable limit.

can't add more than 9 variables to the rewrite

         

JoaoJose

1:11 pm on Dec 13, 2005 (gmt 0)

10+ Year Member



Hi everyone,

I'me doing some url rewriting and I need to use some 40 variables. It wasn't working so after checking the code countless times I realized the server isn't accepting more than 9 variables for the rewrite.

Is this possible to overcome?

Thks in advance!

jdMorgan

2:38 pm on Dec 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This limitation is specified in the mod_rewrite documentation [httpd.apache.org]. It can usually be overcome by the use of multiple chained RewriteRules, using eight single-value variables per rule, with the ninth reserved to 'carry' all remaining variables to the next rule.

For example, this code converts a twelve-variable hyphen-delimited static URL beginning with "products" to its dynamic equivalent, calling "script.php" with variables named v1 through v12:


RewriteRule ^product([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-(.+)$ /script.php.$9?v1=$1&v2=$2&v3=$3&v4=$4&v5=$5&v6=$6&v7=$7&v8=$8 [C]
RewriteRule ^script\.php\.([^-]+)-([^-]+)-([^-]+)-([^-]+)-$ /script.php?val9=$1&v10=$2&v11=$3&v12=$4 [QSA,L]

The first rule moves variables 1 through 8 to the query string, saving the remainder of the requested variables in $9 as a 'tail' on the script.php URL-path, and then chains to the second rule.
The second rule then extracts variables 9 through 12, and appends them to the existing query string.

You should be able to use five chained rules to handle 40 variables in a similar manner.

Jim

JoaoJose

5:57 pm on Dec 13, 2005 (gmt 0)

10+ Year Member



thks Jim I knew you'd help me out. :)

JoaoJose

9:04 pm on Dec 13, 2005 (gmt 0)

10+ Year Member



Well the code isn't working. I think I pretty much understand it so I made some changes I think you missed but it still doesn't work.

the original code

RewriteRule ^product([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-(.+)$ /script.php.$9?v1=$1&v2=$2&v3=$3&v4=$4&v5=$5&v6=$6&v7=$7&v8=$8 [C]
RewriteRule ^script\.php\.([^-]+)-([^-]+)-([^-]+)-([^-]+)-$ /script.php?[b]val9[/b]=$1&v10=$2&v11=$3&v12=$4 [QSA,L]

the altered code

RewriteRule ^product([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)-(.+)$ /script.php.$9?v1=$1&v2=$2&v3=$3&v4=$4&v5=$5&v6=$6&v7=$7&v8=$8 [C]
RewriteRule ^script\.php\.([^-]+)-([^-]+)-([^-]+)-([^-]+)[b]no dash[/b]$ /script.php?[b]v9[/b]=$1&v10=$2&v11=$3&v12=$4[b]&[/b] [QSA,L]

jdMorgan

9:39 pm on Dec 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, no dash is correct. v1 through val9 are arbitrary, and of course, you'll be changing them to match your real variable names. No trailing ampersand is needed, since [QSA] will take care of that, too.

In order to make debugging this easier, you could temporarily change it to an external redirect so you can 'watch' it work. But you can only change the last rule to a 301 without interfering with the chained rule function.

Jim

JoaoJose

10:15 pm on Dec 13, 2005 (gmt 0)

10+ Year Member



thks very much for your time Jim I'll see if I can work this out

JoaoJose

4:02 pm on Dec 14, 2005 (gmt 0)

10+ Year Member



Ok it's working but I had to delete the [C] . Do you think this can cause any problem?