We have a server that is configured to serve several sites.

One of these sites is a WordPress site, and some of its pages are really Java web applications hosted by Tomcat. The Java web applications make a request to localhost to get the headers and footers so they when their pages are loaded, everything matches the WordPress parts of the site.

This has been causing some interesting configuration challenges lately, as we have also recently tried implementing "default" sites to catch traffic that apache has no other config for.

What happens now is that when the Java applications request header.php from localhost, the default site (whose ServerName is localhost) returns nothing, because there is no such file in the directory where the default site's content resides.

I tried fixing this with a redirect, so that the Java application would be redirected to the correct URL for the header like this:

     RewriteEngine on
     RewriteRule ".*header\.php" "http://site1.mycompany.com/common-files/header.php"
     RewriteRule ".*footer\.php" "http://site1.mycompany.com/common-files/footer.php"

This fixed things slightly: Now when I try to load a Java-based page, I don't get an error, but I get:

Found. The page has moved here

(where "here" is a link to header.php)

This is still a problem since I need the content of header.php, not link to the header.php.

Is there some way to make this more seamless so that the Java applications don't even know they've hit a redirect?

Ideally, I'd modify the Java applications to handle this a little better, but that's out of scope (not my applications, not my authority to go changing them).

  • I configure my default site to serve a 404 Not Found. If somebody hits my web server with an unknown host name, they should get an error. Jul 6, 2017 at 8:09
  • @StephenOstermiller: We have several sites and it was decided to serve a custom page with a list of valid sites hosted by that server. Jul 6, 2017 at 14:11
  • Then maybe don't use the same framework for just that page. Just configure a single static page for that purpose. You could even have configured as the 404 page. Jul 6, 2017 at 14:13
  • @StephenOstermiller: I think I may have confused you, sorry. The default site and page load fine, exactly as they are supposed to. The problem is that now because there is a default site configured for the ServerName "localhost" the Java applications are unable to get their headers and footers (they are not a part of the default site). Things worked fine before the default site was added. Jul 6, 2017 at 14:16

2 Answers 2


The WordPress header and footer are not necessarily designed to be accessed directly. Even if that works, it wouldn't be beneficial for the client to have to make their own calls to them. Also, it seems a bit sloppy to build parts of your site on separate servers.

I would recommend trying to find a different solution. If you have to pull content from your Java site over the internet, one option may be to have your WordPress site serve a mostly empty page, maybe with a small comment in place of the body, and then you could pull that directly with Java and replace that comment with your new body before serving it.

However, you mention that the servername for the resource is "localhost". That has two important implications. First, that the host may not handle calls to "localhost" the way you expect because of VirtualHost entries, so see if that's an issue. However, the more important factor is that you're on the same server with the script.

Rather than making calls across the internet, which take time and have overhead, you could gather the data directly from the PHP script. For instance, you could make a system call to php giving the full path to the script. You might need to set some environment variables, like HTTP_HOST and REQUEST_URI, but whatever you do directly through PHP is going to be cleaner and faster than having to deal with a network connection and a web server, and your client won't have to get involved.

  • Unfortunately, rewriting these Java application is well outside the scope of what I'm supposed to be doing. The WordPress site serves content on some pages, but other "pages" are really being served by Java applications hosted by Tomcat, and using the header and footer to unify the look and feel of everything. Jul 6, 2017 at 13:57

It seems like I need to do the rewrite as proxy, since I was rewriting to a URL. So far, this seems to be working fine:

 RewriteRule ".*header\.php" "http://site1.mycompany.com/common-files/header.php" [P,L]
 RewriteRule ".*footer\.php" "http://site1.mycompany.com/common-files/footer.php" [P,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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