0

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?

6
Contributors
6
Replies
11
Views
13 Years
Discussion Span
Last Post by illegal3alien
0

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.

0

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.

0

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.

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.

0

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

RewriteRule ^cart/(.*)/?(.*)/?(.*)  cart.php?make=$1&model=$2&year=$3
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.