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

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
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.

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