Skip Navigation

Tagged: 

This topic contains 0 replies, has 1 voice.

Last updated by Jerzy Afanasjew 1 month, 2 weeks ago.

Assisted by: Mateus Getulio.

Author Posts
August 7, 2024 at 12:56 pm #16047976

Jerzy Afanasjew

Background of the issue:
I want to set up full site CDN for my website example.eu with WPML translation domain i.e. example.es. I need to use a separate subdomain as origin for the CDN, so I set up origin.example.es and configure that in WPML settings as Spanish language domain. Origin.example.es works without issues, but I want my visitors to use it via example.es domain. So via DNS record I point example.es at my CDN URL, which caches origin.example.es. I expect WPML to be compatibile with full site CDN, its a standard solution.

When accessing example.es I expect to see the same content as at origin.example.es, but with all URLs referring to example.es, not pointing to origin.example.es.

Symptoms:
If I pass Host header (to update all origin.example.es links to example.es domain), WPML forces redirect to my main language version example.eu. But if I do not pass Host header, all URLs at example.es point to origin.example.es. I have verified this to be a WPML issue by using custom code to force WPML to accept Host header. Unfortunately, that code only helps display part of translations, like menu, but not most of the content.

// Function to handle WPML language based on host header
function wpml_handle_host_header_language() {
if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
$languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
foreach ($languages as $lang) {
if (strpos($lang['url'], $host) !== false) {
do_action('wpml_switch_language', $lang['code']);
break;
}
}
}
}
add_action('wp', 'wpml_handle_host_header_language', 0);

I have verified my delivery domain works by accessing it directly for static file URLs, as well as using custom code to force relative URLs.

function buffer_start() {
ob_start('replace_urls_with_relative');
}
function buffer_end() {
ob_end_flush();
}
function replace_urls_with_relative($buffer) {
$site_url = site_url();
return str_replace($site_url, '', $buffer);
}

// Start output buffering on init
add_action('init', 'buffer_start');
// End output buffering before sending the page
add_action('shutdown', 'buffer_end');

Unfortunately I cant use this code because it breaks some plugins.

Questions:
How can I configure WPML to properly handle Host header different from host defined in WPML for that language, without redirecting to the main language version?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.