1

I freshly installed a WordPress site and I configured it behind a reverse proxy with site URL - http://example.com/hello

Problem

I can reach the admin dashboard page, however I cannot go further as the link becomes http://example.com/wp-admin instead of http://example.com/hello/wp-admin. How could I get rid of this weird behaviour?

Details that might help...

  1. I am quite sure that the WordPress Address (URL) and Site Address (URL) are configured as http://example.com/hello, I even checked the database table wp_options.
  2. When it arrives the admin dashboard page with correct CSS, the browser will show the correct link for half seconds and suddenly change to the wrong one. I even checked the network log in my Chrome and Firefox, the requested URL is http://example.com/hello/wp-admin.
  3. I have tried to configure the .htaccess as well, but no luck.

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /hello
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /hello/index.php [L]
    </IfModule>
    # END WordPress
    
3

Eventually, I found the reason of this weird behaviour.

It is caused by the JavaScript embedded in wp_admin_canonical_url() (https://developer.wordpress.org/reference/functions/wp_admin_canonical_url/)

The URL is determined by this piece of code

$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']

where the latter one returns wp-admin instead of hello/wp-admin.

By manipulating the value in $_SERVER['REQUEST_URI'], I can successfully get the correct behaviour in admin dashboard. But I still hope for an elegant solution without touching WordPress code.

0
add_filter('set_url_scheme', 'hacky_uri_fix', 10, 2);
function hacky_uri_fix($url, $scheme) {
    if ( $url === {{BAD ADMIN URL}} ) {
        $url = {{CORRECT ADMIN URL}}
    }
    return $url
}

should work, just replace the bad URL you are trying to avoid with the correct one. I'm sure there's a better solution to this out there but I'm not super familiar with subfolder installs and this should work without touching core code.

-1

Victor is 100%, so save the next person (maybe even myself) work digging further:

  • the function wp_admin_canonical_url is in the ./wp-admin/includes/misc.php file
  • Change

    $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']

to be

$_SERVER['HTTP_HOST'] . '/news/. $_SERVER['REQUEST_URI']

Where /news can be anything you have as a path in front of the wp-admin folder.

  • Please don't ever edit core files. This is bad and will break if you update Wordpress (which you should be doing for security updates at the minimum) – mrben522 Jan 10 at 19:54
  • Then what is the path forward? My whole site including all of the admin panels work great EXCEPT for the left hand panel. It cuts off the "/news" path. You can neg my answer and his find as well. but until something else "core" comes along and works this is a VALID. WORKING. work around. – Chad Jan 10 at 20:19
  • Follow that function down the rabbit hole until you find a filter you can hook on to. I think the hook you're looking for is gonna be set_url_scheme – mrben522 Jan 10 at 20:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.