Skip Navigation

Resolved

Resolved in: 3.7.0

Overview of the issue

Since WPML 3.6.3 some users are experiencing incorrect browser redirections when the Language URL format is configured on WPML -> Languages page with the following settings:

  • Different languages in directories
  • Use directory for default language => enabled

Workaround

This problem will be addressed in the next WPML version.

In the meantime we have a patch you can apply to the class-wpml-canonicals-hooks.php file, found in the ../wp-content/plugins/sitepress-multilingual-cms/classes/canonicals/ folder.

Replace the following whole block of code, around line 34.:

public function redirect_pages_from_root_to_default_lang_dir() {
	if ( is_page() ) {
		$current_path   = wpml_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
		$canonical_url  = get_permalink( get_queried_object_id() );
		$canonical_path = wpml_parse_url( $canonical_url, PHP_URL_PATH );
		$canonical_path = $this->get_paginated_canonical_path( $canonical_path );

		if ( $current_path !== $canonical_path ) {
			$current_query = wpml_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
			parse_str( $current_query, $current_query_array );
			$canonical_url = add_query_arg( $current_query_array, $canonical_url );
			$this->sitepress->get_wp_api()->wp_safe_redirect( $canonical_url, 301 );
		}
	}
}

With:

public function redirect_pages_from_root_to_default_lang_dir() {
    global $wpml_url_converter;
    if ( is_page() && ! WPML_Root_Page::is_current_request_root() ) {
        $lang = $this->sitepress->get_current_language();
        $current_uri = $_SERVER['REQUEST_URI'];
        $abs_home    = $wpml_url_converter->get_abs_home();
        $install_subdir = wpml_parse_url( $abs_home, PHP_URL_PATH );
        $actual_uri  = preg_replace( '#^' . $install_subdir . '#', '', $current_uri );
        $actual_uri  = '/' . ltrim( $actual_uri, '/' );

        if ( 0 !== strpos( $actual_uri, '/' . $lang . '/' ) ) {
            $canonical_uri = trailingslashit( $install_subdir ) . $lang . $actual_uri;
            $canonical_uri = user_trailingslashit( $canonical_uri );
            $this->sitepress->get_wp_api()->wp_safe_redirect( $canonical_uri, 301 );
        }
    }
}

29 Responses to “Redirection issues with "languages in directories" and "use directory for default language"”

    • Hello @massimoM-6,

      Thanks for your comment. We are currently investigating this issue for edge cases and we’ll update the errata as soon as possible.

      Regards,
      Pierre

  1. Hi, this patch fixed the exact problem that I was having. I was searching for hours and luckily came across this.

    What I really came here to leave a message about was a more effective way of informing users about patches and bugs, and to clean up old info. The WPML forum contains a wealth of info about bugs and how they were fixed/patched, but over time, information about the problems or the fixes themselves become outdated. My suggestion is for these old posts to be closed, and a link to the latest information or errata to be provided. This would save hours or searching and using outdated information.

    For example, in my particular case, I tried several things with translating the WC endpoints vs leaving them untranslated etc, but if I had seen this latest post first, I would not have had to gone through those troubleshooting steps.

    I’m not sure if there’s a newsletter of some sort, but if there’s some way to “push” fix notifications to users (paying customers with valid support license), that would be much appreciated.

  2. Hello, i also had this problem using last version of WooCommerce, all Woocommerce endpoints were broken, meaning no acccess to users accounts and paiement.

    The above fix seemed to fix it.

    There should be a way to be warned with this kind of problems, because i spent hours looking for a solution ! But thanks for the fix!

  3. Hello Fortune Rice and fabriceV-2,

    Currently, we have a “‘Known issues and their solutions” section at the bottom of each support page with a link to the errata main page. On this page, we have a parametric search to find the right issue.

    The errata should be updated and marked as resolved when a fix is released in a new version.

    Generally speaking, the support team is responsible for pointing the users to the right errata.

    I will pass your suggestions to our team to see what can be done to improve it.

    Thanks,
    Pierre

  4. Hi, I had this issue with WooCommerce. It would redirect users to an empty cart after checkout (with the message: Checkout is not available whilst your cart is empty). This patch fixed the issue, thanks.
    Lucky I found this one, the wpml forums are a jungle 🙂

  5. Hello

    The my account page no longer works for me. Reading through several threads here, I did the following:

    – I tried the above, though without results.
    – I tried installing the latest beta, without results.

    kursiv-software.com/mein-konto should lead to my account, but now even creates a server error.

    I’m stuck – any suggestions appreciated.

    Karsten

  6. OMG, thanks – not three hours of my life, just about one 🙂 I found my own hack in the meantime, completely removing redirect_pages_from_root_to_default_lang_dir action from template_redirect hook — i suppose it should not break anything in URLs resolving — if I am not using Browser language redirect, right? –Jan

  7. Yeah same error here, could not reach the woocommerce endpoints like
    “/en/my-account/orders” always get redirected to “/en/my-account”.

    Have found myself the function “redirect_pages_from_root_to_default_lang_dir” was creating the redirect and so found this thread.

    Fixed it temporarily by commented out line 26 in “class-wpml-canonicals-hooks.php”

    //add_action( 'template_redirect', array( $this, 'redirect_pages_from_root_to_default_lang_dir' ) );

  8. Hi team,
    greats fix.
    I’m searching the solution since few days after a customer say me that’s a problem with account page.
    The fix is ok for me.
    Many thanks
    Nicolas

  9. This fix should happen automatically once the code has been replaced, correct? I’ve replaced mine, but my Farsi content is still being redirected to English.

  10. I did the patch job via FTP but it didn’t work at all.
    Now I cannot even login to WordPress and get the following error message:
    Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /homepages/23/d504965203/htdocs/new.allimonecolodge.com/wp-content/plugins/sitepress-multilingual-cms/inc/request-handling/redirection/wpml-rootpage-redirect-by-subdir.class.php on line 47

    Warning: include(http://www.allimonecolodge.com/new.allimonecolodge.com/wp-content/languages/): failed to open stream: no suitable wrapper could be found in /homepages/23/d504965203/htdocs/new.allimonecolodge.com/wp-content/plugins/sitepress-multilingual-cms/inc/request-handling/redirection/wpml-rootpage-redirect-by-subdir.class.php on line 47

    Warning: include(): Failed opening ‘http://www.allimonecolodge.com/new.allimonecolodge.com/wp-content/languages/’ for inclusion (include_path=’.:/usr/lib/php7.0′) in /homepages/23/d504965203/htdocs/new.allimonecolodge.com/wp-content/plugins/sitepress-multilingual-cms/inc/request-handling/redirection/wpml-rootpage-redirect-by-subdir.class.php on line 47

    Please help!

  11. Hello Juliette Kando,

    As far as I can see, you have this warning only on the root page, probably because you have set a non-existing “HTML page” in WPML -> Language URL Format. I think this warning is occurring only on the root page and your backend is still accessible.

    Can you try to access the admin page WPML -> Language URL format and check that you have a valid HTML file in the input? Otherwise, you can also select the option “A page” and create a root page inside WordPress.

    If the issue persist, I invite you to open a ticket on the support forum to get a more dedicated assistance.

    Thanks,
    Pierre

    • Hi pierre,

      Thank you Pierre for your prompt reply.

      No, the error message occurred on all the pages. even when I tried to login to my WP dashboard. By chance, via “history” I managed to get into the site via allimonecolodge.com/en. The problem is that all our publicity is on allimonecolodge.com so I’ve had to disable WPML until this issue is resolved. I’ve been struggling with this for over 2 days now and am at my wits end.

      Is there anyone who can install WPML on our website so it works for English and Spanish with the English version remaining allimonecolodge.com (witout the /en extension) and the Spanish version allimonecolodge/es?

      Your help would be much appreciated,

      Juliette