Solved

Apache re-write rule problem with "?"

Posted on 2006-06-09
4
228 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Maintain Parameters on URL throughout Site (PHP) 6 36
Problem with .htaccess file 4 69
instanceof  operator in java 26 91
Problem to get function 52 106
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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now