Home›Support›English Support›[Escalated to WPML Developers team] Language URL directories cause broken redirects for REST API requests using the `rest_route` query p...
[Escalated to WPML Developers team] Language URL directories cause broken redirects for REST API requests using the `rest_route` query p...
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.
Our wait time is higher than usual, please make sure you are meeting the minimum requirement - https://wpml.org/home/minimum-requirements before you report issues, and if you can take a look at current Known Issues - https://wpml.org/known-issues/. Thank you.
Background of the issue:
I am trying to access a REST API endpoint using the non-permalink form of the link as described in the WP developer docs (https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/#routes-vs-endpoints): "On sites without pretty permalinks, the route is instead added to the URL as the rest_route parameter. For the above example, the full URL would then be hidden link;. On this site, I have followed the instructions to set up WPML to use Different Languages in Directories (https://wpml.org/documentation/getting-started-guide/language-setup/language-url-options/#different-languages-in-directories) and "Use directory for default language" (https://wpml.org/documentation/getting-started-guide/language-setup/directory-default-language/). I should note that simply using the pretty permalink version of the URL is not an option. I was able to fix the issue by modifying line 556 of `sitepress.class.php` in version 4.7.1 to the following: if ( ! defined( 'WP_ADMIN' ) && isset( $_SERVER['HTTP_HOST'] ) && ! empty( $_SERVER['HTTP_HOST'] ) && $_SERVER['HTTP_HOST'] && ! defined( 'WP_CLI' ) && did_action( 'init' ) && ! $isLoadingATEAutomaticTranslationWidget && empty( $_GET['rest_route'] ) { Note the addition of the rest_route check at the end. I'm not sure that this the best solution however. Perhaps a bit more sophisticated version of this would work.
Symptoms:
The URL is redirected to a broken version (hidden link) which WP no longer recognizes as a REST API request and outputs: {"code":"rest_no_route","message":"No route was found matching the URL and request method.","data":{"status":404}}.
Questions:
How can I fix the broken redirects for REST API requests using the rest_route query parameter?
Is there a more sophisticated solution than modifying line 556 of sitepress.class.php?
If the issue persists we still need your help implementing whatever you do on a clean installation to be able to see the result to be able to troubleshoot:
hidden link
By the way we are aware of your code suggestion and we thank you for that. Before considering touching the core code we need to have a clear replication on a clean installation to rule out any exceptions.
Hi Christopher. When posting this issue, I hoped to keep things a bit simple as the central issue was WPML attempting a redirect when it shouldn't be. I'll be more specific.
The TL;DR issue is attempting calls using the rest_route URL query parameter and not the REST API pretty permalink URL causes WPML to do a 302 redirect when following documented setup guides for using language directories (and possibly other configs). This redirect is a bug in WPML which breaks many of these calls, particularly authenticated calls and non-GET method calls (such as PUT, POST, and DELETE) as the 302 redirect causes the request body to be lost, resulting in the loss of all non-URL-parameter data.
Since posting technical details here isn't reliable, I created a Github gist with the key details that a developer can use to better see the issue. hidden link
"1) A URL like this even does not work when WPML is not active:"
Sorry, I got the URL wrong. It should be:
hidden link
The URL doesn't matter so much though. I was only using it as an example that WPML is doing a redirect for a request to the REST API when it should leave the request alone. This is made clear in the gist I linked to above.
"2) On the site neoncat.party there is an immediate 302 redirection."
I did this because it matches the config of the original customer that reported this issue to us. However, I discovered that this does not affect this report since removing this modification does not change the behavior of the WPML redirect for REST API requests.
"Would you please setup a Root page following the steps mentioned in the documentation below?"
The site already had that change. You can see the settings for the test site in the attached image.
"If the issue persists we still need your help implementing whatever you do on a clean installation to be able to see the result to be able to troubleshoot:"
The issue will persist because it is a bug in the logic of your code. I gave the key configuration details in my original request and included the debug output from the plugin. I don't know what more I can provide, so I created an admin user on the site that you can test. You can find the login details here:
hidden link
I should note that asking us to change the WPML config to fix this issue is not an acceptable solution. I'm contacting you on behalf of a mutual customer who reported an issue to my company. I dug into the issue and found this bug. Fixing this specific bug in WPML resolved the issue our mutual customer reported to us. This customer told us that they are not willing to change any WPML configs to fix the issue.
Christopher is currently unavailable. I have confirmed that the issue is on our development team's list, but there are no updates at this time. We will keep you informed here as soon as there is more information.