•
•
•
•
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
![]() |
•
•
Join Date: Jun 2004
Location: Phoenix Arizona
Posts: 115
Reputation:
Rep Power: 5
Solved Threads: 2
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:
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: 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?
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
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 :-)
•
•
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,892
Reputation:
Rep Power: 32
Solved Threads: 110
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:
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.
Take this rule, for example:
RewriteRule ^cart/(.*)/$ cart/index.php?n=$1
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.
Dani the Computer Science Gal
Do you run a computer-related website? Feature it in our niche link directory!
Do you run a computer-related website? Feature it in our niche link directory!
•
•
Join Date: Jun 2004
Location: Phoenix Arizona
Posts: 115
Reputation:
Rep Power: 5
Solved Threads: 2
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.
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 :-)
•
•
Join Date: Jan 2007
Posts: 1
Reputation:
Rep Power: 0
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Linux Servers and Apache Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
apple bbc browser cellphone choose computer core debian dell desktop development enterprise fedora fiji france games gentoo gpl hardware ibm install internet kernel laptop linux microsoft mobile news novell open open source openoffice operating operating system os preinstall ps3 red hat rhel security server source spoof sun system ubuntu unix vista web windows
- Arrays (C++)
- I need help on my 'address book' assignment! (C++)
- Using Search Engine Friendly PHP URLs (PHP)
- SQL Injection Attack (Database Design)
Other Threads in the Linux Servers and Apache Forum
- Previous Thread: Forbidden | You don't have permission to access /~username on this server.
- Next Thread: Trouble with .htaccess & redirecting dynamic URLs



Linear Mode