Support » Plugin: Polylang » [Plugin: Polylang] Language homepage redirection problem and solution (but incomplete)

  • Thank you Chouby and everyone who contributes to this plugin.
    This was my first attempt to make a WordPress powered site/template in various languages and really love the way Polylang works, with different permalinks for each language and letting you decide what to translate. It’s really flexible and not obstructive. A very intelligent approach. I love the way it works with widgets.

    My settings are:
    – Remove /language/ in pretty permalinks. Example: http://lisboaautentica.com/en/
    – Hide URL language information for default language
    – Add language information to all URL including posts, pages, categories and post tags (not recommended)

    My permalink structure is:
    /%year%/%monthnum%/%day%/%postname%

    Notice the missing trailing slash. I prefer URLs of the type http://domain.com/about-us (instead of http://domain.com/about-us/).

    Now, the only problem i found was when i was visiting the homepage for some language (say “/es”) the “redirect_canonical” filter in “core.php” was responding with a 404 and redirecting to “/es/” (with trailing slash).

    So I changed the filter hook to this:

    function redirect_canonical($redirect_url, $requested_url) {
    	//return $requested_url == home_url() || $requested_url == $this->page_link('', get_option('page_on_front')) ? false : $redirect_url;
    	return is_home() || $requested_url == $this->page_link('', get_option('page_on_front')) ? false : $redirect_url;
    }

    This solved the 404/redirect problem.
    But now I have 2 valid URLs for the language homepage:
    “/es” and “/es/” both give me the same content. And this is not good for SEO.

    I’, trying to fix this, but still didn’t find the best way to do it.
    Any help will be appreciated and i hope it contributes to this plugin.

    Other info: I’m using Nginx and WordPress Multisite.

    PS: i just made my donation 😉

    http://wordpress.org/extend/plugins/polylang/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Chouby

    (@chouby)

    Thank you for this report. I will look at this.

    Plugin Author Chouby

    (@chouby)

    Well that’s true that the link generated for the translated homepage is without trailing slash whereas the canonical link (you are redirected too) has the trailing slash. In fact:

    1. When translating the front page, Polylang tells WordPress that the translated page is THE front page (but Polylang does not create the link as the front page).
    2. WordPress ALWAYS adds a trailing slash to the front page.

    Moreover, the filter ‘redirect_canonical’ is not called when requesting a canonical URL. So here WordPress will consider that /es/ is the canonical URL and so if you request it, you can’t filter it in ‘redirect_canonical’. What I propose, to do what you want, is to make the redirection before WordPress:

    add_action('template_redirect', 'my_template_redirect', 1); // before redirect_canonical is called
    function my_template_redirect() {
    	$requested_url  = is_ssl() ? 'https://' : 'http://';
    	$requested_url .= $_SERVER['HTTP_HOST'];
    	$requested_url .= $_SERVER['REQUEST_URI'];
    	$redirect_url = trim($requested_url, '/');
    	if (is_front_page() && $redirect_url != $requested_url && $requested_url != home_url('/')) {
    		wp_redirect($redirect_url);
    		exit;
    	}
    }
    
    add_filter('redirect_canonical', 'my_redirect_canonical', 1, 2); // before Polylang filter is called
    function my_redirect_canonical($redirect_url, $requested_url) {
    	if (is_front_page())
    		return false; // prevents chain redirection
    }

    I am still not sure if I should implement this… or just add the trailing slash for the home pages links just as WordPress does… (to avoid 404 error log before redirection).

    Thank you Chouby for answering. I’m sure whatever you decide will be a good choice. This particular problem isn’t that serious.

    I did just found a serious problem. I was going to make a new post, but here it goes:
    When you hit the homepage for the default language with a query string (“domain.com/?” or “domain.com/?key=val”) “curlang” doesn’t get set and every post in every language get’s shown.

    I accidentally found this because mailchimp (for example) adds a query string to links back to our site. And the homepage showed the sidebars with all widgets, ignoring the language setting.

    The solution is simple:
    in “inlude/core.php” (line 247) replace:
    if (empty($query->query) && home_url('/') == trailingslashit((is_ssl() ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])) {
    with:
    if (empty($query->query) && home_url('/') == trailingslashit((is_ssl() ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].str_replace('?'.$_SERVER["QUERY_STRING"], '', $_SERVER['REQUEST_URI']))) {

    This will make the first pass through “pre_get_posts” filter to correctly identify we are on the homepage by striping the query_string (if there is one) and set “curlang” correctly.

    My settings are (still):
    – Remove /language/ in pretty permalinks. Example: http://lisboaautentica.com/en/
    – Hide URL language information for default language
    – Add language information to all URL including posts, pages, categories and post tags (not recommended)

    Note: this doesn’t happen int the homepage for languages other than the default (“domain.com/es?key=val” is ok)

    Please confirm this problem and correct it in a future version.

    Thank you and best regards.

    Plugin Author Chouby

    (@chouby)

    Thank you for the bug report. I will include your proposed modification in v0.8.3.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: Polylang] Language homepage redirection problem and solution (but incomplete)’ is closed to new replies.