WPML

问题概述

使用 Yoast SEO Pro 插件时,RegEx 重定向规则在不同语言中的工作方式并不相同。默认语言和次要语言的行为存在差异。

例如,如果您创建了一个将 ^/en/faq/(.+)$ 重定向到 /en/support/$1 的规则,当尝试通过 http://example.com/en/faq/test/ 访问时,它可能无法按预期工作。

临时解决方法

这个问题已修复。 更新到最新版本即可在您的站点上解决此问题,因此,只有当您暂时无法更新时,才需要此临时解决方法。

选项 1

作为快速的临时解决方法,您可以创建不同的规则。
按照最初的示例:
  • ^/faq/(.+)$ 重定向至 /support/$1
  • ^faq/(.+)$ 重定向至 /en/support/$1

选项 2

在继续操作之前,请确保对您的站点进行了完整备份。
  • 打开 …/wp-content/plugins/wp-seo-multilingual/classes/class-wpml-wpseo-redirection.php 文件。
  • 找到第 34 行。
  • 将以下代码:
    foreach ( $redirections as $redirection ) {
        if ( $redirection['origin'] === $url || '/' . $redirection['origin'] === $url ) {
            return true;
        }
    }
  • 替换为:
    foreach ( $redirections as $redirection ) {
    				if($redirection['format'] == 'regex'){
    					// Ensure $url starts with /, if regex would need to check if string starts with this is needed.
    					if(strpos($url, '/', 0) !== 0){
    						$url = '/' . $url;
    					}
    					// Lets use ~ as a regex delimiter instead of /, as we are matching a URL.
    					preg_match('~' . $redirection['origin'] . '~', $url, $matches); 
    					if(count($matches) > 0){
    						return true;
    					}
    				} else if($redirection['origin'] === $url || '/' . $redirection['origin'] === $url){
    					return true;
    				}
    }

所有已知问题

仍然遇到此问题?

如果更新无法解决问题,请提出支持问题,我们将与您一起排查。