How to extract QUERY STRING from HTTP_REFERER and append it to target URL

Hello,

I want to extract the QUERY STRING from HTTP_REFERER and append it to target URL using MOD_REWRITE module in apache configuration file. Could anyone please tell me how to achieve this? Any help will be heartily appreciated.

Best wishes,
Amit.

Amit,

I don’t know what affect Tomcat may have on things and I’d have to take the time to lookup what 307 signifies. Using 301, however, will force the browser to display the redirection so, if that upsets you, leave it off.

G’nite!

Regards,

DK

Hello DK,

Thank you for your reply. I have got the setting to work with R=307, however the browser displays ugly redirect message. Is there anyway to prevent this?

Your help has been great. Sorry to bother you time and again.

Best wishes,
Amit Parmar.

Amit,

The QSA flag will retain an existing query string but using the mere existence of a query string to prevent a loop won’t help that at all. HOWEVER, in the past, I’ve used a “marker” placed in a query string to prevent looping. Essentially, add a key=value pair such as x=marker when you first redirect and check for x=marker in the query string to prevent looping.

POST? Not really. The POST array is handled differently. The GET array is created by the query string so it can be easily managed.

Regards,

DK

Hello,

Just adding to my last question. Can I POST with RewriteRule?

Now when the form is submitted to the Tomcat after R 301, the form values are not obtained at the server side result into input error!

Please note that even if I am redirecting, my server does not change. The server remains say www.example.com?Id=001&mtbl=101.
Then the user will remain on the same server on subsequent requests handled by Tomcat, say www.example.com/inquiry/index.jsp?Id=001&mtbl=101.

It seems to be working with R=307 but the browser displays redirect alert on each submit!

Sorry to bother you.

Best wishes,
Amit.

Hello DK,

One more problem I am truck with. Some of the URLs in my site have query strings. I need to append the referer’s query string to the request query string.
With RewriteCond %{QUERY_STRING} ^$, this does not work. And as you have commeded above, removing this condition results into looping. Can you please suggest a way to acheive the foresaid result.

Thank you in advance.
Amit.

Hello DK,

Thank you for your reply. Have a good day!

Amit.

Amit,

I’m not a TomCat user so I have no experience with its connectors. A quick review of their documentation would indicate, however, that you can configure the connector in any way you see fit. This MAY mean that you can do this but I have no way to verify that it can be done. Sorry.

{Note: Please consider this the answer to the PM, too.}

Regards,

DK

Could you please help me on one more issue? I am using mod_jk connector. Calls to some of my URLs directly go to tomcat through mod_jk. When this be the case, the RewriteRule is not called and the tomcat handles the request. Is there anyway to configure the Rewrite stuff such that the request is processed before ultimately handled by Tomcat. I tried to search the net but could not find a solution. Thank you in advance.

Amit,

Horray! A new question!

RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
# check for empty query string - to prevent looping if nothing else
RewriteCond %{HTTP_REFERER} \\?(.*)$
# capture the query string (after the ?)
RewriteRule .? %{REQUEST_URI}?%1 [R=301,L]
# Don't bother to capture the {REQUEST_URI} - you already have it
# %1 is the first atom captured in the associated RewriteCond statements

If you need to learn more about mod_rewrite, may I suggest my signature’s tutorial?

Regards,

DK

Hello DK,

Thank you so much for your very very very quick answer. I am really preasuntly surprised for the quickness of your reply.

I have two related questions. I am trying to solve them myself. If I can’t, shall ask you.

Thank you so much,
Amit.