0

I have a working reverse proxy from one server to a WP Engine install. We are trying to get it to work with a single URI for the admin and front end. I am trying to determine if there is anything that can be done in WordPress to fix this issue. Or, if anyone has an idea what might cause this issue in the reverse proxy setup. I unfortunately don't have the specific reverse proxy settings.

  1. https://example.com is on one server that I do not have access to.
  2. https://example.com/blog sends traffic to the WP Engine server.
  3. On the WPE server side, we have the below plugin code and wp-config.php setup:

plugin:

# 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']);
}

wp-config.php:

if ( isset($_SERVER['HTTP_X_FORWARDED_SERVER']) ) {
    $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_SERVER'];
    $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_X_FORWARDED_SERVER'];
}
if ( $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ) {
    $_SERVER['HTTPS']       = 'on';
    $_SERVER['SERVER_PORT'] = 443;
}
define('WP_HOME', 'https://example.com/blog');
define('WP_SITEURL', 'https://example.com/blog');

Everything works really well...almost. Some of the admin URLs do a funny redirect. It only happens in the admin. The two examples I have seen are below:

EXAMPLE 1

When a non-logged-in-user requests the wp-admin. WordPress tries to redirect to wp-login.php.

  1. Request: https://example.com/blog/wp-admin
  2. Output from wp_redirect(): https://example.com/blog/wp-login.php?redirect_to=https%3A%2F%2Fexample.com%blog%2Fwp-admin%2F&reauth=1
  3. The actual response the browser gets:

Notice that it is switching to http and it duplicates the reverse proxy directory into blogblog.

I have seen the actual output of wp_redirect, and it is correct, but the browser gets a completely messed up response from the server.

~:$ curl -I -X GET https://example.com/blog/wp-admin/
HTTP/1.1 302 Found
Accept-Ranges: bytes
Cache-Control: max-age=0, must-revalidate, private
Cache-control: no-cache="set-cookie"
Content-Type: text/html; charset=utf-8
Date: Mon, 15 Apr 2019 14:17:56 GMT
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Location: http://example.com/blogblog/wp-login.php?redirect_to=https%3A%2F%2Fexample.com%blog%2Fwp-admin%2F&reauth=1

EXAMPLE 2

The behavior is the same, but I just wanted to add another example of when the behaviour happens. This time it is when Gutenberg tries to save. I click on "Update", and it tries to save the post.

https://example.com/blog/wp-admin/post.php?post=1073&action=edit&meta-box-loader=1&_wpnonce=1234567890&_locale=user

Tries to redirect here:

http://example.com/blogblog/wp-admin/post.php?post=1073&action=edit&message=4

Same problems as above.

Weirdly, the autosaves work fine...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.