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
This issue is fixed in WPML 4.6.9. Updating solves it on your site, so you only need this workaround if you can’t update yet.
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:
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:
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 ); } }