Solved

Apache re-write rule problem with "?"

Posted on 2006-06-09
4
229 Views
Last Modified: 2010-03-04
I'm trying to capture the name value pair of an incoming URL (example: /products/en_US/bf2armor?sourceid=123). This is an apache rewrite rule. The old rules is ^/products/([^/]+)/([^/]+), which for example, accept /products/en_US/bf2armor. This regular expression does not accept any parameters at the end. If you want to add url parameter, it is not as trivial as we think. What this rule does is takes the first ([^/]+) as $1 var and second one as $2 var and return to this url, /reg/entry/subscribe-entry.jsp?prodid=${uppercase:$2}&ipath=15&locale=${language:$1|$1}. This url rewrite rules allow us to accept any locales and product id so we don’t have to hardcode it for every products, else url rewrite will grow linearly.

According to their document, RewriteRule ^/products/([^/]+)/([^/]+)\?(.*)$ /reg/entry/subscribe-entry.jsp?prodid=${uppercase:$2}&ipath=15&locale=${language:$1|$1}&$3 [R], should work because \ is the escape character, but it doesn’t.
0
Comment
Question by:894359
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
ID: 16875843
No,

mod_rewrite doesn't do querystrings. mod_rewriting is for rewriting urls/refrences to files, not for rewriting querystrings.

You can pass key/value pair through POST of course and submit it to the mod_rewrite'd url.

If your server is also running PHP you can also use $_SERVER['REDIRECT_URL'] or similar $_SERVER property to get the url before it was rewritten. You then get the querystring by using parse_url, and parse_str functions to get to the properties.

Hope this helps,

-r-
0
 

Author Comment

by:894359
ID: 16879827
NO PHP... JAVA shop (JSP)
0
 
LVL 10

Assisted Solution

by:sleep_furiously
sleep_furiously earned 250 total points
ID: 16887990
RewriteRule does not compare or match against the query string, but mod_rewrite does have mechanisms for working with query strings.

It looks like you just want to add variables to the query string and preserve the existing query string that came in as part of the URL.

If you want to append the query string that you have made in the rewritten URL to the one that was there before (which it appears might take care of your particular request), add the flag [QSA].  Take out the \? and everything after that from the RewriteRule pattern.

So from your example above, make it like this:

RewriteRule ^/products/([^/]+)/([^/]+)   /reg/entry/subscribe-entry.jsp?prodid=${uppercase:$2}&ipath=15&locale=${language:$1|$1} [R,QSA]


If you need to do something more fancy, like matching a substitution group in the query string, you can use RewriteCond with the QUERY_STRING server variable.

To make a backreference to a grouped part of the last RewriteCond matched, use % instead of $, which backreferences the RewriteRule.

To give a simple example

RewriteCond %{QUERY_STRING} (.*)myvar=([^&]*)
RewriteRule  ^/dirname/([^/])  /someother/$1?newvar=%1

To change /dirname/foo?myvar=bar into /someother/foo?newvar=bar
0
 
LVL 10

Expert Comment

by:sleep_furiously
ID: 16887997
oops, that %1 should be %2, to match the second group from the immediately preceding RewriteCond.  But I think you get the idea.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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