Solved

Optional Additional Get Variables

Posted on 2007-03-20
2
710 Views
Last Modified: 2012-05-05
I have this in my htaccess, which works fine:
RewriteRule ^product/(.*)/(.*)/(.*).php$ /naughty/products/product.php?DPID=$2

I would like to be able to pass 'additional' optional get variables. so I was thinking something like (this does not work though):
RewriteRule ^product/(.*)/(.*)/(.*).php\??(.*)$ /naughty/products/product.php?DPID=$2&$4
0
Comment
Question by:sypder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
smidgie82 earned 500 total points
ID: 18760132
The problem is that the RewriteRule directive only matches its regex against the URL, not the query string.  So, when you visit, for instance, http://example.com/product/1/2/3.php?color=gray, your RewriteRule only sees "/product/1/2/3.php".

If you want "color=gray" to be appended, as your rule indicates, you should do the following instead:
RewriteRule ^product/(.*)/(.*)/(.*).php\??(.*)$ /naughty/products/product.php?DPID=$2&%{QUERY_STRING}

Of course, if there was no query string initially, the new URL will look like "/naughty/products/product.php?DPID=2&", which probably is not what you want.  So instead, you can use the QSA flag, and any part of your new URL that Apache recognizes as part of a query string (i.e., anything after '?') will be _appended_ to the existing query string, instead of replacing it:
RewriteRule ^product/(.*)/(.*)/(.*).php\??(.*)$ /naughty/products/product.php?DPID=$2 [QSA]

I haven't tested the above for syntactic correctness, but the reasoning is correct.  I hope that helps!
0
 
LVL 3

Author Comment

by:sypder
ID: 18761755
Perfect!

Just for archival purposes. the final syntax I used was:

RewriteRule ^product/(.*)/(.*)/(.*).php$ /naughty/products/product.php?DPID=$2 [QSA]
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question