I’m using the WPML plugin to serve versions of the site in different languages, from different domains.
In the trellis config I have my English and French domains both as entries of the same site under site_hosts
. I can access the site from both domains, and the language is being set properly, except with one exception:
If I try to go to whatever is configured in Wordpress as the “main page” in the non-default language, I am 301 redirected to the main page in the default language. This problem does not exist on any non-main pages, and if I switch the “main page” configuration to another page, the problem moves with it.
I delved in and stepped through lines of code, and the redirection is coming from PHP. In particular, from the redirect_canonical
functionality in Wordpress. It is deciding that the home page is wanted, and then looking up the canonical home page URL and redirecting to this. No matter which language is active it’s deciding the English URL is correct, and so redirecting from a legit French home URL to that.
I reached out to WPML support with this issue but they’ve been of very limited use. See the thread here if interested.
As described in that thread, I eventually came up with a workaround:
add_filter('home_url', 'homeUrlFilter', 10, 4);
function homeUrlFilter($url, $path, $orig_scheme, $blog_id)
{
// FIXME: I couldn't solve an issue where the main page always redirects
// to the English version, so this is a nasty workaround.
if ($path === '/') {
return http_build_url($url, ['host' => $_SERVER['HTTP_HOST']]);
}
return $url;
}
I’m wondering now if it could be a Trellis issue, whether something I’ve misconfigured or a bug, or lack of support for my use case.
Any insight would be greatly appreciated. Is there any information I can provide which might give clues?