- 状态
- 等待作者
- 报告针对
- WPML SEO 2.1.0
- 相关插件/主题
- Yoast SEO
- 主题标签
- Compatibility
问题概述
使用 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; } }