Skip Navigation

Resolved

Reported for: WPML Multilingual CMS 4.6.8

Resolved in: WPML 4.6.9

Topic Tags: Bug

Overview of the issue

Users with WordPress installed in a sub-folder and using the WPML feature Use directory for default language are experiencing incorrect URL generation for taxonomy archives and custom post type links. This results in doubled paths in the URL and the consequently 404 errors when trying to access to these pages.

For example, you will get redirected to http://www.domain.com/subfolder/en/subfolder/en/category/new-category/ instead of http://www.domain.com/subfolder/en/category/new-category/.

Workaround

Please, make sure of having a full backup of your site before proceeding.

  • Open …/wp-content/plugins/sitepress-multilingual-cms/classes/canonicals/class-wpml-canonicals-hooks.php file.
  • Look for line 87.
  • Change:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    public function redirectArchivePageToDefaultLangDir() {
        global $wp_query;
     
        $isValidForRedirect = $wp_query->is_archive() && ! call_user_func( $this->is_current_request_root_callback );
     
        if ( ! $isValidForRedirect ) {
            return;
        }
     
        $currentUri = $_SERVER['REQUEST_URI'];
        $lang       = $this->sitepress->get_current_language();
     
        if ( 0 !== strpos( $currentUri, '/' . $lang ) ) {
            $canonicalUri = user_trailingslashit(
                $this->url_converter->get_abs_home() . '/' . $lang . $currentUri
            );
     
            $this->redirectTo( $canonicalUri );
        }
    }
     
    private function redirectTo( $uri ) {
        $this->sitepress->get_wp_api()->wp_safe_redirect( $uri, 301 );
    }
  • For:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    public function redirectArchivePageToDefaultLangDir() {
        global $wp_query;
     
        $isValidForRedirect = $wp_query->is_archive() && ! call_user_func( $this->is_current_request_root_callback );
     
        if ( ! $isValidForRedirect ) {
            return;
        }
     
        $currentUri = $_SERVER['REQUEST_URI'];
        $lang       = $this->sitepress->get_current_language();
     
        if ( 0 !== strpos( $currentUri, '/' . $lang ) ) {
            $realhome = get_option('siteurl');
            $parsed = parse_url($realhome);
            if (false !== strpos($currentUri, $parsed["path"])){
                return;
            } else {
            $canonicalUri = user_trailingslashit(
                $this->url_converter->get_abs_home() . '/' . $lang . $currentUri
            );
            }
     
            $this->redirectTo( $canonicalUri );
        }
    }

3 Responses to “WPML - Double URL slug with Sub-folder Installation and "Use Directory for Default Language" Option”

    • Hey there,
      I can’t provide you an ETA, but this is definitely in our developers roadmap.
      I’ll update this erratum as soon as we have a new version with a fix coming.