Waiting for author
Reported for: WPML SEO 2.1.0
Overview of the issue
When using the Yoast SEO Pro plugin, RegEx redirect rules do not work in the same way across different languages. There is a different behavior for the default language and the second language.
For example, if you create a redirection rule like ^/en/faq/(.+)$ to /en/support/$1, it might not work as expected when trying to access it through http://example.com/en/faq/test/.
Workaround
Option 1
As a quick workaround you can create different rules.
Following the initial example:
- ^/faq/(.+)$ redirects to /support/$1
- ^faq/(.+)$ redirects to /en/support/$1
Option 2
Please, make sure of having a full backup of your site before proceeding.
- Open …/wp-content/plugins/wp-seo-multilingual/classes/class-wpml-wpseo-redirection.php file.
- Look for the line 34.
- Replace this code:
foreach ( $redirections as $redirection ) { if ( $redirection['origin'] === $url || '/' . $redirection['origin'] === $url ) { return true; } } - With:
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; } }

I have problem with the Hreflang of this web and i cannot create the redirecctions 301 in spanish 🙁
Hello Raffaela,
I’m not sure that it is the same issue reported here. Have you tried any of the workarounds provided?
If that’s the case and the problem persists, please open a chat in our assistance channel. We have there all the required tools to help you better.
Regards
Is it possible to add this code to the official plugin release from your side? It will be problematic to add code fix to every my website after your plugin release.
This issue in in our devs roadmap. We will update this page once a fix is released.
Thank you for your understanding.
Very hope that this will be fixed in nearest realease. Will wait.
Thank you!
Hi, recently, the issue with Yoast Seo redirects occurs on one of my sites, only with urls in language folders, for example /ES/ and /DE/.
What can I do about this? Is the solution under option 2 still advised?
Thank you in advance for your help!
Hello,
I would recommend the option 1 (create different rules per language) instead of modifying the plugin’s code. Did you try that already? If it doesn’t work, please open a new chat on our support forum, our supporters are here to help you out.
The issue is resolved now. The redirects in language folders weren’t working as expected because WordPress redirected them before the Yoast SEO Premium redirection could take effect.
WordPress has a redirect rule that redirects a 404 to a related page on the website. To prevent this guess redirect behavior from WordPress, I have added this filter to functions.php:
add_filter(‘do_redirect_guess_404_permalink’, ‘__return_false’);
What seems essential is that after adding the filter, I resaved the WordPress permalinks and cleared all caches. (Otherwise, every redirect returns a 404)
Hi,
I’m glad to hear the issue is resolved now! Thanks for pointing out the resaving the permalinks and clearing the cache steps.