Skip to content

Using WordPress 2.8 and up with a proxy webserver in front

I use a public plain apache server with mod_rewrite to serve my wordpress as i don’t trust php enough to expose it to the world 😉

I wrote and updated a previous post describing what i’ve done to make things work for me, but now with WordPress 2.8 it’s broken, so i had to figure out a fix for this problem again. This is how i’ve done it:

on my public webserver i use this line in the virtual host config:

RewriteRule ^(.*)$ http://sotf.labnet.uclv.net/wordpress$1 [L,P]

The magic is done in my wp-config.php file:

/* That's all, stop editing! Happy blogging. */

// hack for using wordpress behind another apache / mod_rewrite
// config hack:
// set subdir of this worpress installation absolute to this webserver (normally /wordpress)
$local_subdir = '/wordpress';
// set subdir on remote server (normally /)
$remote_subdir = '/';

//print_r($_SERVER);
if ( isset( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
$old_host = $_SERVER['HTTP_HOST'];
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
$_SERVER['SCRIPT_URI'] = str_replace($old_host, $_SERVER['HTTP_X_FORWARDED_HOST'], $_SERVER['SCRIPT_URI']);
}
$_SERVER['REQUEST_URI'] = str_replace($local_subdir.'/', $remote_subdir, $_SERVER['REQUEST_URI']);
$_SERVER['SCRIPT_URL'] = str_replace($local_subdir.'/', $remote_subdir, $_SERVER['SCRIPT_URL']);
$_SERVER['SCRIPT_URI'] = str_replace($local_subdir.'/', $remote_subdir, $_SERVER['SCRIPT_URI']);
$_SERVER['SCRIPT_NAME'] = str_replace($local_subdir.'/', $remote_subdir, $_SERVER['SCRIPT_NAME']);
$_SERVER['PHP_SELF'] = str_replace($local_subdir.'/', $remote_subdir, $_SERVER['PHP_SELF']);
// end of hack

if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
require_once(ABSPATH . 'wp-settings.php');

IMPORTANT: the hacking of the $_SERVER variables has to be done before requiring wp-settings.php!

Update

I had some trouble that rewriting pretty permalinks didn’t work at first. The problem was that I had to enable the use of .htaccess for my wordpress directory in the apache config of my local server:

<Directory /data01/www/wolfgang.reutz.at />
 AllowOverride FileInfo
</Directory>

And i had to enter the public webaddress into both of the blog url fields in the general settings of the wordpress admin interface. After that i had to manually tweak my .htaccess file in my wordpress installation:

# BEGIN WordPress
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
# END WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.