Solved

Apache re-write rule problem with "?"

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

13 Experts available now in Live!

Get 1:1 Help Now