User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Linux Servers and Apache section within the Tech Talk category of DaniWeb, a massive community of 402,962 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,682 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Linux Servers and Apache advertiser: Programming Forums
Views: 9223 | Replies: 4
Reply
Join Date: Jun 2004
Location: Phoenix Arizona
Posts: 115
Reputation: Arizona Web is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Arizona Web's Avatar
Arizona Web Arizona Web is offline Offline
Junior Poster

multiple variables in a mod_rewrite URL string

  #1  
Jul 5th, 2004
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?
Need a website designer? arizona web design : phoenix web design : MCP Media intelligent web design and web development solutions. MCP Media is owned and operated by Chris Hooley - who happens to be a real nerd... on purpose :-)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,892
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 110
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: multiple variables in a mod_rewrite URL string

  #2  
Jul 5th, 2004
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.
Reply With Quote  
Join Date: Jun 2004
Location: Phoenix Arizona
Posts: 115
Reputation: Arizona Web is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Arizona Web's Avatar
Arizona Web Arizona Web is offline Offline
Junior Poster

Re: multiple variables in a mod_rewrite URL string

  #3  
Jul 6th, 2004
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.
Need a website designer? arizona web design : phoenix web design : MCP Media intelligent web design and web development solutions. MCP Media is owned and operated by Chris Hooley - who happens to be a real nerd... on purpose :-)
Reply With Quote  
Join Date: Aug 2004
Posts: 1
Reputation: felix243 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
felix243 felix243 is offline Offline
Newbie Poster

Re: multiple variables in a mod_rewrite URL string

  #4  
Aug 16th, 2004
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.
Reply With Quote  
Join Date: Jan 2007
Posts: 1
Reputation: valiik is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
valiik valiik is offline Offline
Newbie Poster

Help Re: multiple variables in a mod_rewrite URL string

  #5  
Jan 10th, 2007
Originally Posted by cscgal View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Linux Servers and Apache Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Linux Servers and Apache Forum

All times are GMT -4. The time now is 7:11 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC