Super User is a question and answer site for computer enthusiasts and power users. 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

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

My problem is I have a URL like, but it should actually point to

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?

share|improve this question
You'd probably get better answers on – MDMarra Oct 2 '09 at 11:12
I don't understand the question. You say "but it should actually point to"; 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
up vote 2 down vote accepted

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.

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.