I have a subdomain:


I forcibly redirects to directory:


By changing site URL and some RewriteRule on .htaccess.

My .htaccess:

    <IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /blog/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]

    RewriteCond %{HTTP:X-Forwarded-Host}i ^example\.com
    RewriteCond %{HTTP_HOST} ^blog\.example\.com$
    RewriteRule ^/?(.*)$ https://www.example.com/blog/$1 [L,R=301,NC]

    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^/?(.*)$ https://www.example.com/blog/$1 [L,R=301,NC]

My wordpress address and site address are:

WordPress Address (URL): /blog

Site Address (URL): https://www.example.com/blog

Now the website working fine, but I found an error in wp-admin canonical url on all admin pages:

Uncaught SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'blog.example.com/wp-admin/index.php'; cannot be created in a document with origin 'example.com'; and URL 'example.com/blog/wp-admin/index.php';

When I dig more I found the canonical link is still subdomain( blog.example.com ) :

<link id="wp-admin-canonical" rel="canonical" href="http://blog.example.com/wp-admin" />
        if ( window.history.replaceState ) {
            window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );

Is there any solution for changing this canonical url from https://blog.example.com to https://www.example.com/blog

  • Any luck I have this too when using https its showing http:// Commented Oct 11, 2016 at 19:34
  • I also have a wrong wp-admin-canonical url ... did you ever find a fix for this?
    – higuita
    Commented Feb 2, 2017 at 22:25
  • I couldn't find a solution for this. Instead I moved my wordpress setup to amazon instance and database to RDS. Then Stop using subdomain redirection and directly pointed /blog location to the amazon instance. All good now. Commented Sep 19, 2018 at 10:21

2 Answers 2


wp-admin-canonical is broken, as it assumes how WordPress is installed.

there was a plugin to fix it, but the plugin was removed from the plugin repository apparently. It is still on github and pluginmirror though: https://github.com/wp-plugins/remove-wp-canonical-url-admin-hack http://www.pluginmirror.com/plugins/remove-wp-canonical-url-admin-hack/

  • On version wordpress version 4.8.1, it looks like this is not needed anymore, but older versions may still need this, so at least lets document it
    – higuita
    Commented Aug 21, 2017 at 21:06


The code below didn't really worked for me, as it adds a correct canonical tag but I wasn't able to remove the wrong one with remove_action.

Also, I found that pagination and sorting links in post and page lists had the wrong URL on it as well.

So the solution has been much simpler, just add $_SERVER['HTTP_HOST'] = 'www.yoursite.com'; anywhere in wp-config.php.

Previous answer

I've overrided the wp-admin-canonical by adding this hooks in functions.php:

remove_action( 'admin_head', 'wp_admin_canonical_url' );
add_action( 'admin_head', 'wp_set_admin_canonical_url' );

function wp_set_admin_canonical_url() {
    $removable_query_args = wp_removable_query_args();

    if ( empty( $removable_query_args ) ) {

    // Ensure we're using an absolute URL.
    $current_url  = admin_url( preg_replace( '#^[^?]*/wp-admin/#i', '', $_SERVER['REQUEST_URI'] ) );
    $filtered_url = remove_query_arg( $removable_query_args, $current_url );
    <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
        if ( window.history.replaceState ) {
            window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );

I've taken the code from this pull request: https://github.com/WordPress/wordpress-develop/pull/1504

  • 1
    thank you, it works like a charm
    – aRTURIUS
    Commented Mar 3, 2023 at 15:29
  • but it was doubling up the resulting code so i commented out add_action( 'admin_head', 'wp_admin_canonical_url' ); in wp-admin/includes/admin-filters.php
    – Mazzy
    Commented Oct 11, 2023 at 0:21
  • You should not edit Wordpress Core files as your changes will be overrided on the next update.
    – campsjos
    Commented Oct 11, 2023 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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