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?

Recommended Answers

All 6 Replies

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.

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.

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.

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.

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.