?
Solved

Apache re-write rule problem with "?"

Posted on 2006-06-09
4
Medium Priority
?
232 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

762 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