Solved

Optional Additional Get Variables

Posted on 2007-03-20
2
702 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
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Redirect from old domain to new domain 19 75
LAMP problem identifier tool ? 9 122
Generic Rule to Redirect and Rewrite URL using .htaccess 8 84
wamp versus xampp 4 63
As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 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