title: How to rewrite / proxy an Apache URI to an application listening on a specific port / server?
body: <p>They say that Apache's mod_rewrite is the swiss-army knife of URL manipulation, but can it do this?</p>
  
  <p>Lets say I want to add a new application to my Apache webserver, where the only configurable option of the app is a port number.</p>
  
  <p>I want to use &amp; give out URLs of the form "<a href="http://hostname.example.com/app" rel="noreferrer">http://hostname.example.com/app</a>" rather than "<a href="http://hostname.example.com:8080" rel="noreferrer">http://hostname.example.com:8080</a>". This would ensure that clients would be getting through the institution's firewall as well, and it's generally tidier.</p>
  
  <p>My application includes absolute URIs in php, javascript and css, so I want to prepend my own root location to the URI in the applications internal links. I have no access to DNS records and so can't create another name-based virtual server.</p>
  
  <p>Using Apache's mod_rewrite and mod_proxy modules, I can transparently redirect a client to the correct home-page of the application. But links within that homepage don't point a client to links relative to the new base URL.</p>
  
  <p>So, what's the best way of proxying a request to an application that is listening on a specific port?</p>
  
  <p>For example, if I had an application listening on port 8080, I could put this in my Apache configuration:-</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
      SSLProxyEngine On
      ServerName myhost.example.com
      RewriteEngine On
      UseCanonicalName On
      ProxyVia On
      &lt;Location "/application"&gt;
          RewriteRule ^/application/?(.*) http://localhost:8080/$1 [P,L]
      &lt;/Location&gt;
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>This would work fine if the application didn't use absolute URLs, but it does. What I need to do is rewrite URLs that are returned by the application's css, javascript and php.</p>
  
  <p>I've looked at the ProxyPass and ReverseProxyPass documentation, but I don't think these would work..?</p>
  
  <p>I've also come across Nick Kew's <a href="http://apache.webthing.com/mod_proxy_html/" rel="noreferrer">mod_proxy_html</a>, but this isn't included in the standard Apache Distribution, and my institution's webserver seems to have been fine for years without it.. Other than trawling manually (or using a grep -r | sed type expression) through the application's source code, or using this 3rd party add-on, are there any other ways to go about this?</p>
  
  <p>Could I perhaps use some of the internal server variables in a mod_rewrite rule? For example a rewrite rule based on ’HTTP_REFERER'?</p>