Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client is experiencing issues with regex redirects in Yoast SEO not functioning correctly when used with the WPML plugin. Specifically, redirects that include a language directory (e.g., /de/) fail, resulting in a 404 error.
Solution:
We recommend trying the following workaround:
1. Ensure you have a backup of your site.
2. Open the file

/wp-content/plugins/wp-seo-multilingual/classes/class-wpml-wpseo-redirection.php

in a code editor.
3. Replace the existing loop at line number 34 with the following code:

foreach ($redirections as $redirection) {<br />if($redirection['format'] == 'regex'){<br />if(strpos($url, '/', 0) !== 0){<br />$url = '/' . $url;<br />}<br />preg_match('~' . $redirection['origin'] . '~', $url, $matches);<br />if(count($matches) > 0){<br />return true;<br />}<br />} else if($redirection['origin'] === $url || '/' . $redirection['origin'] === $url){<br />return true;<br />}<br />}

4. Save the changes.
**** Important! Please make a full site backup (files and DB) before you proceed with these steps****
Additionally, we have published an errata page with two workarounds for this issue, which can be found here: https://wpml.org/errata/yoast-seo-redirection-regex-rules-differ-across-languages-with-wpml/

If this solution does not resolve your issue or seems irrelevant due to being outdated or not applicable to your case, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If further assistance is needed, please open a new support ticket at WPML support forum.

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by Itamar 1 year, 7 months ago.

Assisted by: Itamar.

Author Posts
June 13, 2023 at 8:44 pm #13820155

marcH-44

I am trying to: regex redirect in Yoast -Yoast tested and confirmed unwanted interaction with WPML

Link to a page where the issue can be seen: daosoma.com/de/ab

I expected to see: redirect to daosoma.com/de/transkulturelle-psychotherapie

Instead, I got: 404 not found

--
Discussion and confirmation with Yoast:
Begin forwarded message:

From: Yoast Support <support@yoast.com>
Subject: Re: regex redirects in Yoast – manual needed
Date: 12 June 2023 at 12:46:37 CEST
To: Marc Heusser <marc.heusser@daosoma.com>

Ramon replied
Jun 12, 12:46
Hi Marc,

Thanks for your continued patience.

We can imagine how frustrating it can be when the redirects you've entered in Yoast SEO Premium don't work. Let's investigate this issue further and get to the bottom of this issue.

First of all, we'd like to clarify that the forward slash / doesn't have to be escaped as it's not a special character. You can find the list of Regex special characters on this page. That said, we implemented the regex redirects you provided in your email from June 11 in our testing environment, tested them, and verified they worked as expected. Having a closer look at your website, we noticed you're running the WPML plugin. We then enabled WPML on our test site, and the regex redirects that included a language directory, i.e. /de/ stopped working.

Thank you for bringing this issue to our attention. We've confirmed the problem you reported and have created a bug report for our development team.

Workaround
At this time, there is no known workaround other than using simple redirects instead of regex redirects.

What's next?
Our product team will assess the severity of this problem in relation to other open bug reports and new features. Based on their assessment, the bug report will be given a priority level. Our developers work on the highest priority issues first. Unfortunately, this means we cannot give you an estimate of when they'll start working on your report.

If you have any further information that may affect the prioritization or help our development team solve this bug, please feel invited to reply to this email. As soon as our development team has a clear indication in which release this problem will be solved, we'll send you an email to inform you.

Additionally, we highly recommend reporting plugin conflicts to the developer of the conflicting plugin (WPML in this case) as they may have additional information vital to providing a resolution. Having both teams work on the issue is a benefit to you.

How would you rate my reply?
Satisfaction Rating Icons

Thanks and regards,
Ramon

--
Ramon Bauza
Technical support engineer at Yoast

Marc Heusser replied
Jun 12, 8:09
Hi
Having worked with Unix for a while since many years I am well acquainted with regexes in general but not the specific way yoast implements them. I have tested examples with regex101.com but they do not work. Probably because of a silly omission or small change.
Getting a real specification or man page would be most helpful, also the examples are on my webpage to test. The hosting provider is infomaniak.com one of the largest and technically capable ones in Europe so their stuff just works.
Please give me a better answer.

TIA and best regards

Marc

Victoria replied
Jun 11, 16:54
Hi Marc,

Thank you for getting back to us.

We are reviewing your reply and will get back to you as soon as possible.

Thank you for your patience!

Thanks and regards,
Victoria

--
Victoria McCarter
Support Engineer at Yoast

Want to further optimize your website? Improve your SEO skills with our online SEO training courses!

Have you seen our Help Center? This will probably have the answer you're looking for!
Marc Heusser replied
Jun 11, 9:07
Hi Toyin

an example is not a man page for Yoast redirect regexes. Where is the standard spec to be found?
I’d expect eg matching a / would need to be escaped as \/, but it is not necessary nor does it work it seems in Yoast regex redirects.

de/ab -> de/transkulturelle-psychotherapie does work, redirects hidden link to hidden link (literal match)
de\/ab -> … does not work, hidden link results in not found (escaped special character / as \/)

So entering your example
de/([a-z])x\1x\1 -> de/transkulturelle-psychotherapie on my webpage (daosoma.com) with Yoast Premium does not work, hidden link results in not foundde/a[a-z] -> … does not work, hidden link results in not found
de/a[a-z]+ -> … does not redirect, hidden link results in not found
de/a[a-z]* -> … does not redirect, hidden link and hidden link result in not found
de/a-b -> … works as intended, redirects daosoma.com/de/a-b to hidden link (literal match)
de/a-[a-z] -> … does not work, hidden link results in not found
The last six examples are currently active on the website

What could be wrong?

Please get me more information on how these regex work, if there needs to be a specification ot a delimiter character like / first, etc
I do not understand why escaping the / does not work, I do not understand why a range like [a-z] ie one character between a and z does not match.

Thank you in advance

Marc
Toyin replied
Jun 11, 8:16
Hi Marc,

Thanks for contacting the Yoast support team.

We are sorry to hear that you're not finding enough information about Yoast SEO Premium redirects manager regex support. We can imagine how this could be disappointing for you. Let's sort this out right away!

The redirects manager regex tab support the standard spec. This means you could capture groups and match strings. We even tested the backreference by following the example given here of ([a-c])x\1x\1 that should match axaxa, bxbxb and cxcxc and that worked as well. So, you should be able to use backreference in the regular expression based on your needs.

Unfortunately, since these are very powerful we are unable to create them for you or offer advice as we do not want to put you or us in a position where we suggest something and it breaks your site.

Let us know if there's anything else we can assist you with; we're happy to help.

Thanks and regards,
Toyin

--
Toyin Ogunseinde
Support Engineer at Yoast

Want to further optimize your website? Improve your SEO skills with our online SEO training courses!

Have you seen our Help Center? This will probably have the answer you're looking for!
Marc Heusser sent a message
Jun 10, 20:31
I need a manual page for the Yoast regex redirects – specifying all the grammar of the regex there and the way the regex are being applied as I cannot get some to work which test well with regex101.com – eg / I seem not to need to escape with \/, a matches but [a-z] does not, …
TIA
Marc

June 15, 2023 at 7:53 am #13830083

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi, Marc.

Currently, I cannot see this problem. When I try this URL: hidden link, I get redirected to hidden link.

Is it because you manually inserted the redirection, not as a regex?

In any case, if the problem persists, I suspect the following to cause the problem. In WPML -> Languages -> Language URL format, you have selected the option `Different languages in directories`. And then, you also selected the option `Use directory for default language`. It seems you also added a redirection from hidden link to hidden link. But you did not add any root page or HTML file as we instructed in our guide.

https://wpml.org/documentation/getting-started-guide/language-setup/directory-default-language/#:~:text=Please%20note%20that%20when%20the%C2%A0Use%20directory%20for%20default%20language%20option%C2%A0is%20set%20on%20the%C2%A0WPML%20%E2%86%92%20Languages%C2%A0page%2C%20a%20root%20page%C2%A0must%20be%20used.

Could you please add a root page or an HTML and check if it solves the problem?

Thanks,
Itamar.

June 18, 2023 at 3:42 am #13844513

marcH-44

The issue persists, has no relation to the idea given by WPML, Yoast has confirmed the issue on their test bed.

June 18, 2023 at 9:40 am #13844873

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi, and thanks for trying my suggestions.

I'm unfamiliar with Yoast regex redirections.

Can you please help me replicate the issue on a test site, and then I'll escalate it to our second-tier supporters?

For this, I created a test website with a clean WordPress install. You can access it through this link:

hidden link

With this link, you'll be directly logged in.

I've already configured the test site to have German as the default language and English as the second language. Please add the regex to the Yoast plugin and replicate the issue. When everything is finished, and you can replicate the problem, please let us know.

Thanks,
Itamar.

June 18, 2023 at 10:22 am #13845153

marcH-44

It would be best to talk to Yoast engineers as they have replicated it already as they suggested as it is proven to be an interaction between WPML and Yoast plug-in

June 19, 2023 at 11:54 am #13851047

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi,

This issue has been escalated to our developers.

We have a possible workaround. We cannot know if it will work in your case because we are missing information about it. If you wish, you can try it on your site and see if it works for you. Here is what you need to do.

1. Take a backup of your site in case something goes wrong.

2. Edit the following file with a code editor.

/wp-content/plugins/wp-seo-multilingual/classes/class-wpml-wpseo-redirection.php

3. Replace the following loop in line number 34:

			foreach ( $redirections as $redirection ) {
				if ( $redirection['origin'] === $url || '/' . $redirection['origin'] === $url ) {
					return true;
				}
			}

With this loop:

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;
				}
}

4. Save the changes in the file.

**** Important! Please make a full site backup (files and DB) before you proceed with those steps****

Now the redirections in the other languages should work.

I have no ETA for when this fix will be included in future versions of WPML SEO. But I'll keep you updated here on any news regarding this issue.

Cheers,
Itamar.

June 20, 2023 at 4:41 pm #13862159

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi,

For your information, we've published an errata page that includes two workarounds for this case.

https://wpml.org/errata/yoast-seo-redirection-regex-rules-differ-across-languages-with-wpml/