Skip to content

Instantly share code, notes, and snippets.

@LimeCuda
Last active December 23, 2020 02:53
Show Gist options
  • Save LimeCuda/18b88f7ad9cdf1dccb01b4a6bbe398a6 to your computer and use it in GitHub Desktop.
Save LimeCuda/18b88f7ad9cdf1dccb01b4a6bbe398a6 to your computer and use it in GitHub Desktop.
A simple WordPress plugin to rewrite the URLs for content that is requested via a Reverse Proxy https://fewerthanthree.com/tutorial/wordpress-multisite-reverse-proxy-setup-wpengine/
<?php
/**
* @package Reverse Proxy
*/
/*
Plugin Name: Reverse Proxy
Plugin URI: https://limecuda.com/
Description: Reverse proxy setup for a WordPress multisite in subdirectory mode
Version: 1.1
Author: Josh Mallard
Author URI: https://limecuda.com
*/
/*
Props to James Paden of https://instrumentalapp.com for the original code we based this off of.
https://pressable.com/blog/2015/10/15/reverse-proxy-plugin-for-using-a-hosted-wordpress-site-in-a-subdirectory/
*/
// Change to match the desired subfolder, no leading or tralling slash
define("RP_SUBFOLDER", "blog");
function rp_is_login_page() {
return in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'));
}
if ( $_SERVER["REMOTE_ADDR"] != "127.0.0.1" && !is_admin() && !rp_is_login_page() && $_GET["preview"] != "true" ) {
if ( isset( $_SERVER['HTTP_X_FORWARDED_SERVER'] ) ) {
//From http://stackoverflow.com/questions/772510/wordpress-filter-to-modify-final-html-output
ob_start();
add_action('shutdown', function() {
$final = '';
$levels = count(ob_get_level());
for ( $i = 0; $i < $levels; $i++ ) {
$final .= ob_get_clean();
}
// Apply any filters to the final output
$final = str_replace("http://" . $_SERVER["HTTP_HOST"], "https://" . $_SERVER["HTTP_X_FORWARDED_SERVER"] . "/" . RP_SUBFOLDER, $final);
echo $final;
}, 0);
}
}
@olim-dev
Copy link

Hi,

I'm trying to implement a similar set up but with a single site install as opposed to a multisite.

I tried to follow along the tutorial on: https://fewerthanthree.com/tutorial/wordpress-multisite-reverse-proxy-setup-wpengine/ but I'm confused on a few things.

I don't use "Fastly" so I'm not sure if the steps involving those will affect the set up or not?

Basically, I have rp.originsite.com as my wordpress install and would like to have it served through finalsite.com/origin:

1/ Do I need to change site_url and home_url to be "finalsite.com/origin" or leave them as "rp.originsite.com"
2/ When accessing the RP URL, all my assets are being served through "rp.originsite.com/wp-content/etc..", How do I make it serve through "finalsite.com/origin/wp-content/etc..." without receiving 404 errors.
3/ On the plugin above, the use of "HTTP_X_FORWARDED_SERVER" is related to the configuration of Fastly which I'm not using, how do I set that on my end?

I would greatly appreciate any type of guidance as I've been stuck on this for a couple of days.

Thank you in advance!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment