954,242 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

multiple variables in a mod_rewrite URL string

Hi All, this is killing me and I was wondering if anybody knew how to fix this. What I need to do is mod_rewite URLs to mimich directories for variables. Easy, but what I need is to be able to handle the absence of some pseudo directories in the URL and assume that the value is null if that directory doesn't exist. From what I hear, I need to create multiple rules. Seems easy, so this is the code:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /

RewriteRule ^cart/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$ cart/index.php?Car_Truck=$1&Description=$2&Manufacturer=$3&Make=$4&Model=$5&start=$6&product_id=7

RewriteRule ^cart/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$ cart/index.php?Car_Truck=$1&Description=$2&Manufacturer=$3&Make=$4&Model=$5&start=$6

RewriteRule ^cart/(.*)/(.*)/(.*)/(.*)/(.*)$ cart/index.php?Car_Truck=$1&Description=$2&Manufacturer=$3&Make=$4&Model=$5


RewriteRule ^cart/(.*)/(.*)/(.*)/(.*)$ cart/index.php?Car_Truck=$1&Description=$2&Manufacturer=$3&Make=$4

RewriteRule ^cart/(.*)/(.*)/(.*)$ cart/index.php?Car_Truck=$1&Description=$2&Manufacturer=$3

RewriteRule ^cart/(.*)/(.*)$ cart/index.php?Car_Truck=$1&Description=$2

RewriteRule ^cart/(.*)$ cart/index.php?Car_Truck=$1



Unfortunately I lose all variables, but there are no 404 errors for the various URLs I would use.

I am able to make it work this this:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /

RewriteRule ^cart/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$ cart/index.php?Car_Truck=$1&Description=$2&Manufacturer=$3&Make=$4&Model=$5&start=$6&product_id=7


by specifying the directory as 0, but the resulting URLs are not as clean. then I would have to specify URLs like this doimain.com/cart/car/0/0/0/0/0/0/
instead of doimain.com/cart/car/

any ideas what I am doing wrong here?

Arizona Web
Junior Poster
118 posts since Jun 2004
Reputation Points: 16
Solved Threads: 2
 

This just came to me. Not sure if this is definitely the problem, but here's a suggestion. When you have 7 wildcards separated by slashes, Apache knows that there are 7 variables and each is separated by a slash, and therefore can figure out what each variable is. But when you have multiple rules, Apache gets confused. For example, what if the first variable is SUPPOSED to be dani/daniweb ...

Take this rule, for example:

RewriteRule ^cart/(.*)/$	 cart/index.php?n=$1

You could easily fill dani/daniweb into that one variable spot. So when you have multiple variables the way you do, Apache doesn't know if when it sees cart/dani/daniweb whether it should use the rule for one variable or the rule for two variables. It gets even more complex with more variables. Suppose a URL cart/1a/1b/2a/2b/3a ... where you meant to have only variables 1a/1b, 2a/2b, and 3a ... or was that 1a and 1b/2a/2b ??

The reason it works with only one rule is because Apache doesn't have to decide how many variables there are before it can decide which rule to use. With only one rule in the .htaccess file that fits the parameter of beginning with cart/, it knows it has to use that one. Once it knows the rule it has to use, it's easy to just see seven / in the original URL and seven / in the rewritten URL.

I would study up a bit on regular expressions. Instead of just using wildcards (.*) you should set it up as "a variable cannot have a / in it" instead of "a variable can be anything." Therefore, Apache will knows that everytime it sees / it is the end of a variable.

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

I decided (and this isn't the best of the best solutions, but it will work) to rename each rule with a different rule indicator as the first psuedo directory.

If there are 2 variables it will say something like /2/variable/variable/ if there are 3 it will say /3/variable/variable/variable/.

This is going to be a tiny but inefficient, but it isn't going to kill me. If anybody else knows a better way, I am sure there is one, I was just unable to implement it using hte methods I showed above.

Arizona Web
Junior Poster
118 posts since Jun 2004
Reputation Points: 16
Solved Threads: 2
 

I am new to php and I was wondering how to break up the script so that I don't have to code everything in php. I want to do a site with the feel of AISCP.com but I don't know how to mix php with html... I'm sure this is a dumn Q.

felix243
Newbie Poster
1 post since Aug 2004
Reputation Points: 10
Solved Threads: 0
 
I would study up a bit on regular expressions. Instead of just using wildcards (.*) you should set it up as "a variable cannot have a / in it" instead of "a variable can be anything." Therefore, Apache will knows that everytime it sees / it is the end of a variable.



cscgal, you seem to be pretty knowledgeable about this, I am trying to learn the regular expression patterns and my question is, are the regular expressions the same in all the programming languages or does each language has it's own regular expressions? I appreciate it.

valiik
Newbie Poster
1 post since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Just stick a "?" after each slash (matches 0 or 1):

RewriteRule ^cart/(.*)/?(.*)/?(.*)  cart.php?make=$1&model=$2&year=$3
cholmon
Newbie Poster
1 post since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

^cart/([^/]*)/([^/]*)/([^/]*) cart.php?make=$1&model=$2&year=$3

illegal3alien
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: