Skip to content

Instantly share code, notes, and snippets.

@neverything
Last active April 2, 2022 07:49
Show Gist options
  • Save neverything/7675846 to your computer and use it in GitHub Desktop.
Save neverything/7675846 to your computer and use it in GitHub Desktop.
WordPress behind a reverse proxy.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /blog/
RewriteBase /~accountname/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . /blog/index.php [L]
RewriteRule . /~accountname/index.php [L]
</IfModule>
# END WordPress

WordPress behind a reverse proxy

WordPress behind a reverse proxy. Your public domain of the WordPress site is your.example.com/blog and the actual server URL (where WordPress lives is server123.hostingcompany.com/~accountname

Using the following plugin as mu-plugin: https://raw.github.com/mistermarco/wp_reverse_proxy/master/su_reverse_proxy.php

  • WordPress URI: In the database all the URIs including home_url and site_url point to the public domain like your.example.com/blog
  • Reverse Proxy: Needs to send you the $_SERVER['HTTP_X_FORWARDED_HOST'] in order to work.
  • .htaccess: WordPress will eventually try to overwrite the htaccess file for you when saving new permalinks, please block this.
<?php
/*
Plugin Name: Stanford Reverse Proxy
Description: Turn this on after you set up this blog to work with a reverse proxy.
Version: 1.1
Author: Marco Wise
Author URI: http://www.stanford.edu/dept/its/
Copyright (c) 2007-2011 Board of Trustees, Leland Stanford Jr. University.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of Stanford University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
# Disable the Canonical Filter
remove_filter('template_redirect', 'redirect_canonical');
# Strip out extraneous path information from REQUEST_URI
# but only if page is requested from a reverse proxy
if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$su_rewritebase = $_SERVER['SCRIPT_NAME'];
$su_rewritebase = preg_replace('/\/(?:wp\-admin\/)?[-\w]*\.php$/', '', $su_rewritebase);
$pattern = preg_replace('/\//', '\/', $su_rewritebase);
$_SERVER['REQUEST_URI'] = preg_replace("/$pattern/", '', $_SERVER['REQUEST_URI']);
}
?>
@edorivai
Copy link

edorivai commented Oct 25, 2017

@sthembi, I can only guess, but I think

RequestHeader set X-Forwarded-Host publicdomain.com/blog

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