我有一个子域:
https://blog.example.com/
我强制重定向到目录:
https://www.example.com/blog
通过更改网站URL和一些重写规则。htaccess。
我的.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]
</IfModule>
我的wordpress地址和网站地址是:
WordPress Address (URL): /blog
Site Address (URL): https://www.example.com/blog
现在网站运行正常,但我在所有管理页面的wp admin规范url中发现了一个错误:
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\';
当我进一步挖掘时,我发现规范链接仍然是子域(
blog.example.com ) :
<link id="wp-admin-canonical" rel="canonical" href="http://blog.example.com/wp-admin" />
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, document.getElementById( \'wp-admin-canonical\' ).href + window.location.hash );
}
</script>
是否有任何解决方案可以更改此规范url
https://blog.example.com 到
https://www.example.com/blog
SO网友:campsjos
更新下面的代码对我来说不太管用,因为它添加了一个正确的规范标记,但我无法用remove_action
.
此外,我发现文章和页面列表中的分页和排序链接上的URL也有错误。
因此,解决方案要简单得多,只需添加$_SERVER[\'HTTP_HOST\'] = \'www.yoursite.com\';
任何地方wp-config.php
.
上一个答案我已经覆盖了wp-admin-canonical
通过在中添加此挂钩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 ) ) {
return;
}
// 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 ); ?>" />
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, document.getElementById( \'wp-admin-canonical\' ).href + window.location.hash );
}
</script>
<?php
}
我从这个拉取请求中获取了代码:
https://github.com/WordPress/wordpress-develop/pull/1504