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

  • 1
    You'd probably get better answers on serverfault.com – MDMarra Oct 2 '09 at 11:12
  • I don't understand the question. You say "but it should actually point to example.com/t/bar" and then you say "But that doesn't preserve the URL, and instead just redirects to the Tomcat URL with the /t/ prefix." Do you want the change the URL or not? – innaM Oct 2 '09 at 11:20
  • Manni, sorry for the confusion. No, I do not want the URL to change. – Kim L Oct 2 '09 at 11:22

You should use the 'P' (proxy) option on the mod_rewrite rule to be done internally to the server. You will also need to enable mod_proxy.

from the mod_rewrite docs:

'proxy|P' (force proxy)

This flag forces the substitution part to be internally sent as a proxy request and immediately (rewrite processing stops here) put through the proxy module. You must make sure that the substitution string is a valid URI (typically starting with http://hostname) which can be handled by the Apache proxy module. If not, you will get an error from the proxy module. Use this flag to achieve a more powerful implementation of the ProxyPass directive, to map remote content into the namespace of the local server.

Note: mod_proxy must be enabled in order to use this flag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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