【问题标题】:Disable www redirect WordPress禁用 www 重定向 WordPress
【发布时间】:2016-04-15 08:29:45
【问题描述】:

我正在尝试禁用从 www.site.comsite.com 的重定向,反之亦然,我使用了 remove_filter('template_redirect', 'redirect_canonical');,但它不起作用.我的 htaccess 是干净的。

【问题讨论】:

  • 不,我不是想将我的网站重定向到非 www,只是想避免那些重定向到 www。或没有 www。
  • 如果您同时设置 www 和非 www,它可能会在 google 搜索结果中复制内容。

标签: php wordpress redirect


【解决方案1】:

只需添加您的网站地址和 WordPress 地址,例如

http://example.com

【讨论】:

  • 不,我不是想将我的网站重定向到非 www,只是想避免那些重定向到 www。或没有 www。
【解决方案2】:

有一种硬核方法可以阻止这种情况,但我个人不喜欢它,因为下次您更新 Wordpress 时,此更改将得到修复。

您可以在第 587 行编辑文件:./wp-includes/canonical.php,然后删除或评论整个部分:

/**
 * Filters the canonical redirect URL.
 *
 * Returning false to this filter will cancel the redirect.
 *
 * @since 2.3.0
 *
 * @param string $redirect_url  The redirect URL.
 * @param string $requested_url The requested URL.
 */
$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );

// // yes, again -- in case the filter aborted the request
if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) {
    return;
}

if ( $do_redirect ) {
    // protect against chained redirects
    if ( ! redirect_canonical( $redirect_url, false ) ) {
        wp_redirect( $redirect_url, 301 );
        exit();
    } else {
        // Debug
        // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
        return;
    }
} else {
    return $redirect_url;
}

【讨论】:

    猜你喜欢
    • 2018-05-26
    • 1970-01-01
    • 2018-11-12
    • 2020-09-05
    • 2016-04-03
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多