How to forward requests to another URL without actually changing the URL in Apache

apache-2.2mod-rewriterewrite

I have Apache running in a domain, for example http:// example.com. I also have Tomcat running on my server and I've forwarded all requests made to http:// example.com/t/ to Tomcat and all other URLs are served by Apache directly.

My problem is I have a URL like http:// example.com/bar, but I want tomcat to process that url, so it should actually point to http:// example.com/t/bar. In other words, the URL should be processed by Tomcat which gets the application context "bar".

Problem is, I'm not quite sure how to make the proper rewrite rule for this. I tried adding this to sites-available/default

RewriteEngine on
RewriteRule ^/bar(.*) /t/bar$1 [R]

But that doesn't preserve the URL, and instead just redirects to the Tomcat URL with the /t/ prefix. If I remove the [R], then I just get a 404.

What is the proper RewriteRule to solve my problem?

Best Answer

You need to proxy it with the [P] option. This also will require you to set up mod_proxy.

See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html and search for "force proxy".

Keep in mind that this will put additional load on your Apache. How much load is dependent upon how much traffic you receive and how quickly the Tomcat server can return traffic back to the Apache.

You could also just simply include /bar in your mod_proxy_ajp rules or mod_jk configuration (JkMount), which would be a far far better solution than using mod_rewrite.

Related Topic