Polylang 语言检测重定向至 301

问题描述 投票:0回答:4

我正在为 WordPress 多语言网站使用 Polylang 插件。 根据 SEO 指南,主页 www.myweb.com 应重定向到 www.myweb.com/en,状态为 301。目前其重定向状态为 302。 函数 home_requested() 可以做到这一点,但我们无法触及插件核心功能。 我尝试为此功能添加过滤器。还尝试为 wp_redirect 函数添加过滤器,但不起作用。 还有其他方法可以解决这个问题吗?谢谢你。

php wordpress polylang
4个回答
2
投票

但是,您可能已经解决了这个问题,为其他希望解决此问题的人发帖。

我通过编辑插件中的choose-lang.php文件解决了这个问题

wp-content->plugins->polylang->frontend->choose-lang.php :277 或对 302 执行 ctrl + f 并将其替换为 301。


2
投票

我测试了下面的代码工作正常。

在functions.php文件中添加以下代码

add_filter('pll_redirect_home', 'my_pll_redirect_home' );
function my_pll_redirect_home( $redirect ) 
{
    header( 'Vary: Accept-Language' );
    wp_safe_redirect( $redirect, 301, POLYLANG );
    exit;
}

1
投票

我测试了functions.php对我不起作用。

nano /polylang/frontend/choose-lang.php(行+265)

将 302 更改为 301。完成。

并且它会在任何 Polylang 更新后自行重置


0
投票

用于过滤主页重定向的插件文档位于此处

正如@kishan-kothari最初建议的那样,您可以使用此功能过滤此重定向。

add_filter( 'pll_redirect_home', 'my_pll_redirect_home' );
function my_pll_redirect_home( $redirect ) {
    header( 'Vary: Accept-Language' );
    wp_safe_redirect( $redirect, 301, POLYLANG );
    exit;
}

但是,您不能在主题文件中使用此挂钩,因为它必须在加载主题之前触发。您可以使用上述功能轻松创建一个 drop-ins 插件或 MU 插件,并且它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.