We’ve got a WordPress project that is setup to run through a reverse proxy. With this setup, we constantly have situations where we need to check whether or not we’re accessing the site from its primary URL or via the reverse proxy.
To do this, we set up a simple conditional to check the header for the appropriate “HTTP_X_FORWARDED_SERVER” and give the appropriate response based on that check.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Conditional to check if we're being served via the Reverse Proxy. | |
* The URL for the Forwarded Server below (example.com) should be the final | |
* URL for the site using the Reverse Proxy. | |
*/ | |
function lc_is_reverse_proxy() { | |
if ( 'www.example.com' === $_SERVER['HTTP_X_FORWARDED_SERVER'] ) { | |
return true; | |
} else { | |
return false; | |
} | |
} |