Mobile app version of vmapp.org
Login or Join
Michele947

: Make local requests from webapp to header and footer files work without redirects for the default website We have a server that is configured to serve several sites. One of these sites is

@Michele947

Posted in: #Apache2 #Redirects

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).

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Michele947

2 Comments

Sorted by latest first Latest Oldest Best

 

@Ravi8258870

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]

10% popularity Vote Up Vote Down


 

@XinRu657

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme