[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Apache re-write rule problem with "?"

Posted on 2006-06-09
4
Medium Priority
?
233 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
[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
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 1000 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 1000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

649 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