I freshly installed a WordPress site and I configured it behind a reverse proxy with site URL -
Problem
I can reach the admin dashboard page, however I cannot go further as the link becomes instead of
. How could I get rid of this weird behaviour?
Details that might help...
- I am quite sure that the WordPress Address (URL) and Site Address (URL) are configured as
, I even checked the database table
wp_options
. - 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
.
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
I freshly installed a WordPress site and I configured it behind a reverse proxy with site URL - http://example/hello
Problem
I can reach the admin dashboard page, however I cannot go further as the link becomes http://example/wp-admin
instead of http://example/hello/wp-admin
. How could I get rid of this weird behaviour?
Details that might help...
- I am quite sure that the WordPress Address (URL) and Site Address (URL) are configured as
http://example/hello
, I even checked the database tablewp_options
. - 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/hello/wp-admin
. 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
- Possibly a plugin? Try disabling them wpbeginner/plugins/… – michalzuber Commented Jun 12, 2017 at 4:47
- Possibly nope, it is a fresh and clean install with only 2 default plugins. – Victor Wong Commented Jun 12, 2017 at 4:49
3 Answers
Reset to default 3Eventually, I found the reason of this weird behaviour.
It is caused by the JavaScript embedded in wp_admin_canonical_url()
(https://developer.wordpress/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.
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.
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.