Apache mod_rewrite question

2019-08-23 10:07发布

问题:

I have an application running under tomcat 6. In front of it I have an apache server which is forwarding all requests. That is, the user "talks" to the apache and not to tomcat. For some reasons the it is requested the following: when the apache server receives a request of the form

http://www.mydomain.com/myApp

then it has to be forwarded to http://www.mydomain.com/$PREFIX/myApp

where $PREFIX is a POST parameter. This $PREFIX parameter is also available as a COOKIE and as a extra header. I didn't find a way using mod_rewrite for reading post parameters/cookies/headers.

Is this possible at all? If not, should I use another apache module/connector?

Thanks in advance.

Luis

回答1:

You can't use POST data for mod_rewrite. This is because the POST data isn't in the HEADER of the http request, it's in the BODY.

My suggestion would be that you perform an action on the posting page that adds the prefix to the URL, which would mean you don't even need to rewrite.



回答2:

try something like (my regex is a bit flakey so may need a bit of tinkering):

RewriteCond %{HTTP_COOKIE} yourcookie=(.*)
RewriteRule ^/myApp(.*)$ /%1/$1 [R,L]

The %1 will backreference to groups in the RewriteCond pattern.

More examples here