1

Let's say my browser issues a request to http://localhost/accounts?memberUuid=<some uuid>

I'd like apache2 to pickup that request, add a custom HTTP header with the uuid in it, strip the uuid param from the URL, and redirect to http://localhost:9000/accounts with the new custom header.

I'm a total apache noob. I have only used it as a simple web server for serving html, css, and javascript files. So I don't even know where to begin, otherwise I'd post some things I've tried so far.

Is this possible with apache2 alone? Do I need some additional tools running to accomplish this? Thanks!

Edit

This makes it look like I should be able to add whatever headers I want to a request. Or am I misunderstanding this? How would I use this along with an incoming value on the URL to send the header I need?

Samo
  • 223
  • 2
  • 10
  • Just to confirm, do you want clients to request the redirect URL with the custom header or do you want to send the custom HTTP header in your response? I don't think you can control client's request HEADER other than http-cookie I'd say. – mask8 Aug 01 '12 at 23:04
  • Yes, I was hoping I could have the clients request the redirect URL with a custom http header. If http-cookie is the only way to add values to the request via apache2, can you please demonstrate this? – Samo Aug 02 '12 at 01:55
  • @mask8: I made an update to my question. It looks to me like you should be able to add request headers of any sort but maybe I'm misinterpreting what I've read there. – Samo Aug 02 '12 at 02:01

3 Answers3

2

This turned out to be possible by combining RewriteRule with Proxy

<VirtualHost *:80>
  RewriteEngine On
  RewriteCond %{QUERY_STRING} ^(.*)memberUuid=(.*)$
  RewriteRule ^/(.*)$ http://127.0.0.1:9000/$1 [E=memberUuid:%2,P]
  ProxyPreserveHost On
  ProxyPass /excluded !
  ProxyPass / http://127.0.0.1:9000/
  ProxyPassReverse / http://127.0.0.1:9000/

  Header add iv-user "%{memberUuid}e"
  RequestHeader set iv-user "%{memberUuid}e"
</VirtualHost>
Samo
  • 223
  • 2
  • 10
1

It's not possible as described. The stateless nature of HTTP means that you cannot associate a current response with a future request unless you use a mechanism such as cookies.

Ignacio Vazquez-Abrams
  • 45,019
  • 5
  • 78
  • 84
  • Thanks. Can you show an example of setting a cookie and doing a redirect? And would it be possible to use a URL parameter as the cookie value? – Samo Aug 02 '12 at 02:25
1

To answer to your edited question about mod_headers, you are probably misunderstanding. The module lets you add additional request headers on server end. You can not force clients to send specific headers freely unless you have a communication rule over HTTP protocol b/w clients and server.

You can probably want to go with Cookie. you can use Set-Cookie header. I am not 100% sure if you can use cookie over sites running on different port, but I thought it could work as long as domain name is the same. About cookie, even wikipedia explains very well.

http://en.wikipedia.org/wiki/HTTP_cookie

EDIT: I personally use php a lot so here is a very simple example to set Cookie and redirect in PHP:

// set cookie value
setcookie('uuid', $_GET['memberUuid'], time() + 3600);
// redirect
header('Location: http://localhost:9000/accounts');
exit;

and you can retrieve the cookie value by $_COOKIE['uuid'] on localhost:9000

EDIT 2:

If you only configure on apache, mod_headers won't be able to do this job since it doesn't handle GET variables as far as I know. Instead, mod_rewrite may work.

RewriteEngine On
RewriteRule ^/accounts\?memberUuid=(.*) http://localhost:9000/accounts [R,CO=uuid:$1:localhost:1440:/]

I didn't test this but I know mod_rewrite is capable of retrieving values from your URL, setting Cookies, and redirection.

mask8
  • 169
  • 5
  • Thanks for your response. Can you show an example configuration that writes a url parameter to a cookie and does a redirect? – Samo Aug 02 '12 at 02:24
  • @Samo I edited my answer. – mask8 Aug 02 '12 at 02:31
  • I was hoping to see what this would look like in an apache site definition. I don't want my server code to know this is happening. It's another system that will be adding this cookie when we go live, and I'm basically trying to mock out that system just using apache. – Samo Aug 02 '12 at 02:54
  • hehe ok @Samo. I updated once again :) – mask8 Aug 02 '12 at 03:21
  • Thanks, that looks a lot closer to what I'm looking for, but again, I'm a total noob. Where do I place these Rewrite directives? I also have a lot of questions about the syntax inside the brackets and the meaning of some of the values (especially 1440) but feel free to link me to something if the explanation is too lengthy! – Samo Aug 02 '12 at 03:57
  • yeah that's a little too much for me to explain here. You can explore some information on the Web to see how to write apache config file and about mod_rewrite. Here is some manual pages http://httpd.apache.org/docs/2.2/configuring.html http://httpd.apache.org/docs/current/mod/mod_rewrite.html – mask8 Aug 02 '12 at 04:37
  • Is there anyway to verify that it set the cookie? When the request gets to my server listening on port 9000, I don't see the cookie. I found some documentation indicating that your syntax is correct, so I'm not sure why I wouldn't be getting the cookie. – Samo Aug 02 '12 at 05:25
  • let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/4364/discussion-between-samo-and-mask8) – Samo Aug 02 '12 at 13:34