Forum Moderators: phranque

Message Too Old, No Replies

Help with Query String in RewriteRule

Handling Multiple parameters in Query String

         

karthikk

9:24 pm on Feb 24, 2010 (gmt 0)

10+ Year Member



Hello,
I have an application running on jboss. The webserver is hostedo n Apache. The request coming into the webserver needs to get redirected to the jboss server.
The issue I am having is handling multiple request parameters. Here is an example:
Page User tries to access:
http://example.com/search?param1=val1&param2=val2&param3=val3

This needs to get redirected to:
http://jboss-server:8080/search?param=val3

In short, I need to be able to extract the val 3 from the Query_String. Am not sure how to do that. been stuck for a long time. Please help!
This is what I have:
RewriteCond %{Query_String} ^(.*)
RewriteRule ^/search$ http://jboss-server:8080/search?param=%1

Obviously it is not right! but I do not know what to do here.

HELP!
Thank you
Karthik

[edited by: jdMorgan at 5:00 am (utc) on Feb 25, 2010]
[edit reason] example.com, de-linked jboss-server [/edit]

g1smd

11:47 pm on Feb 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The RewriteCond pattern matching will need to extract the param3 value, and it will be available in a %n (where is a digit) backreference for use in the substitution.

You need a much better pattern than (.*) and you need to re-use that value once found.

jdMorgan

5:21 am on Feb 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The proper solution is to use a reverse-proxy through-put, not a redirect.

The rest is just matching the "param3" part of the query string and back-referencing it as noted above. If the client may or may not send all parameter-names, then you'll need:

RewriteCond %{QUERY_STRING} ^([^&]*&)*param3=([^&]*)
RewriteRule ^/search$ http://jboss-server:8080/search?param=%2 [P]

otherwise, this might be a bit faster:

RewriteCond %{QUERY_STRING} ^param1=[^&]*&param2=[^&]*&param3=([^&]*)
RewriteRule ^/search$ http://jboss-server:8080/search?param=%1 [P]

The latter code snippet still allows the values of param1, param2, and param3 to be blank. If that's not what you want, change the "*" quantifiers to "+" or use specific numeric quantifiers or numeric-range quantifiers.

With this reverse-proxy set-up, your front-end server should be configured to send the X-Forwarded-For request header to the back-end, and the back-end should be configured to log that header instead of Remote_Addr(ess). In this way, the front-end will send the correct IP address of the requesting client to the back-end for logging (Otherwise all requests to the back-end will be logged as originating at the front-end server).

The main advantages are that the proxy request is local (and therefore likely much faster than a client redirect), and that the back-end server name, port number, and URL-path are not 'exposed' to the client or to search engines.

It's not clear why the param1 and param2 query string parameters are included in the request if they are to be ignored/discarded, so there may be more to this problem than described here... If not, then those parameters should not be required to be sent by the client in the first place.

Jim

karthikk

4:20 pm on Feb 25, 2010 (gmt 0)

10+ Year Member



Thanks a lot guys. Jim, your first option worked like a charm.

Karthik