Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When a user first hits our server, we want to capture some information about the incoming request: GET parameters, Referer header, &c. The general idea is that when we get an incoming request that matches some RewriteConds (doesn't have a cookie, doesn't have a particular GET param in case they don't accept cookies, &c.), we use a RewriteRule with [P] to transparently proxy the request to a servlet (actually a Spring controller, if that matters) that will analyse the incoming request, then send a 302, with a new cookie set, to redirect the user to the originally requested URL. That is,

  • User requests /foo.html
  • mod_rewrite detects that this is the user's first request (no cookie, no GET flag param)
  • A RewriteRule with [P] proxies to /my/spring/controller
  • Servlet analyses the request and responds with a 302 to Location: /foo.html.

The first three steps are simple enough. The problem is that in step four, the servlet has no idea that /foo.html was ever requested, which means (a) it can't record the fact that such was the case (a business requirement) and (b) it doesn't know where to redirect. We can see which server was requested in X-Forwarded-Host et al, but looking at the request URL just shows /my/spring/controller.

What we want to achieve, then, is ideally a proxy pass transparent not just to the client but to the receiving servlet as well.

One option is to pass the URL in an environment variable with something like [E=FOO:REQUEST_URI]. However, this increases the complexity of the servlet, and it's not obvious to me whether the request is otherwise unchanged. The point of the servlet is to analyse the request coming from the client, preferably unchanged by Apache. If mod_rewrite changed the request URL, can I trust all other aspects of the request to be unchanged?

share|improve this question

Trust mod_rewrite, and if paranoid pass the stuff your interested in to your applet, as a series of CGI variables e.g.

RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_COOKIE}  !.........
RewriteRule /?foo.html   /my/spring/controller?theRequest=%{THE_REQUEST}&requestURI=%{REQUEST_URI}&referer=%{HTTP_REFERER} [P,L]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.