• Hi,

    I’m getting nuts with Polylang and the rewrite rules. So what’s the problem:

    I have a link like http://ssn.localhost/de/skater/name/ and a corresponding rewrite rule:

    add_action('generate_rewrite_rules', 'myvar_add_rewrite_rules');
    function myvar_add_rewrite_rules($wp_rewrite) {
    
    $new_rules = array(
    	'data/skater/(.+)' => 'index.php?page_id=814&skater='.		$wp_rewrite->preg_index(1),
    
    	'(de|en)/data/skater/(.+)' => 'index.php?page_id=814&skater='.$wp_rewrite->preg_index(2).'&lang='.$wp_rewrite->preg_index(1),
    	);
    	$wp_rewrite->rules =  $new_rules + $wp_rewrite->rules;
    }

    The second rule was nserted after reading Polylang forums. But whatever I do – it doesn’t work.

    Usually, (and it worked with qtranslate and WPML) there should be a parameter “skater” as query_var, but I allways get forwarded to 404, or when playing a bit with the rule to http://ssn.localhost/de/skater/.

    What is the reason? and how can this be fixed (rewrite analyzer shows the me correct page_id, a skater variable and a lang variable as it should be correct). The rewrite rules were never a problem with other language tools so far.

    Please help!!

    regards,
    Lars

    P.S.: I tried the latest official version and the latest beta version of PL (1.7.2).

    • The language is set from the directory name in pretty permalinks
    • Remove /language/ in pretty permalinks
    • When the front page is visited, set the language according to the browser preference

    https://wordpress.org/plugins/polylang/

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

    (@chouby)

    Hi!

    if your page with page_id 814 is in English and you try to query it together with the German language, it looks clear for me that you will get nothing.

    in qTranslate a page_id is associated to multiple languages. In Polylang one page_id is associated to a unique language.

    Thread Starter fokus3

    (@fokus3)

    Hi,

    unfortunately, that isn’t the reason. I tried both: english and german. Always the same. I get redirected to the page_id 814, but this “skater” argument is not getting passed.

    I far as I could see ist that the “rewrite analyzer” plugin shows me that me rule ist correct and a query_var “skater” ist set correct. Then I created a test function:

    add_action( 'template_redirect', 'change_search_url_rewrite' );
    	function change_search_url_rewrite() {
    			global $wp, $db;
    
    			error_log(print_r($wp,true));
    	}

    simple to see the paramters. Now the pagename is correct, but “skater” is always empty. This only happens when PL is activated.

    Before I used WPML and it worked like a charme.

    So what can it be….?


    Lars

    Thread Starter fokus3

    (@fokus3)

    Hi,

    I made some further tests to try to find a solution, but I havent’t. But perhaps this can help you to give me a piece of support.

    At first I swiched back to WPML (backup) and made a call to

    http://ssn.localhost/de/data/skater/nico-ihle/

    The output from by error_log in template_redirect (see my last post) was:

    [query_vars] => Array
            (
                [page_id] => 814
                [skater] => nico-ihle
            )
        [query_string] => page_id=814&skater=nico-ihle
        [request] => de/data/skater/nico-ihle
        [matched_rule] => de/data/skater/(.+)
        [matched_query] => page_id=814&skater=nico-ihle
        [did_permalink] => 1

    I was forwarded to the correct page. Fine!

    Now I turned off WPML and turned on PL and imported all posts (WPML to PL Importer)

    I called now the same link and get/got:

    [query_vars] => Array
            (
                [page] =>
                [pagename] => data/skater
                [lang] => de
            )
    
        [query_string] => pagename=data%2Fskater&lang=de
        [request] => de/data/skater
        [matched_rule] => (en|de)/(.?.+?)(/[0-9]+)?/?$
        [matched_query] => lang=de&pagename=data%2Fskater&page=
        [did_permalink] => 1

    it redirects me to http://ssn.localhost/de/data/skater/.

    As you can see, there is no more skater parameter in query_vars. My custom rule isn’t been used.

    Next I checked my rewrite rules using the Rewrite analyzer plugin. There I entered de/data/skater/nico-ihle. The plugin shows me, that my custom rule is beeing selected:

    (de|en)/data/skater/(.*?)?/?$ 	
    
            page_id: 814
             skater: nico-ihle
               lang: de

    In other words, WPML and Rewrite analyzer are working as expected, but PL not. I hope that the given information can help you to give me a support or to identify the problem behind.

    Please can you check this and provide a fix or sollution?

    Thank you and regards,
    Lars

    Plugin Author Chouby

    (@chouby)

    I tested your exact code. Of course I get a 404 as I don’t have the rest of your code but the matched rule and matched query are correct (of course I had to add the query var ‘skater’ to fully match the query).

    Thread Starter fokus3

    (@fokus3)

    Hi,

    well it is pretty simple. I send you here the full code so that you can reproduce the problem

    add_filter('query_vars', 'myvar_vars');
    	function myvar_vars($public_query_vars) {
    		$public_query_vars[] = 'skater';
    		return $public_query_vars;
    	}
    
    	add_action('generate_rewrite_rules', 'myvar_add_rewrite_rules');
    	function myvar_add_rewrite_rules($wp_rewrite) {
    
    		$new_rules = array(
    			'(de|en)/data/skater/(.*?)?/?$' => 'index.php?page_id=814&skater='.$wp_rewrite->preg_index(2).'&lang='.$wp_rewrite->preg_index(1),
    
    			'(data/skater)/(.*?)?/?$' => 'index.php?pagename='.$wp_rewrite->preg_index(1).'&skater='.$wp_rewrite->preg_index(2),
    			'(de|en)/(data/skater)/(.*?)?/?$' => 'index.php?pagename='.$wp_rewrite->preg_index(2).'&skater='.$wp_rewrite->preg_index(3).'&lang='.$wp_rewrite->preg_index(1),
    		);
    
    		$wp_rewrite->rules =  $new_rules + $wp_rewrite->rules;
    	}
    
    	add_action( 'template_redirect', 'change_search_url_rewrite' );
    	function change_search_url_rewrite() {
    			global $wp_query, $wp, $db;
    			error_log(print_r($wp,true));
    	}

    Well, this is the code, I’m using to test. The “only” problem is, that when I activate Polylang, my rules are not fire (but they are firing with WPML or rule analyzer etc.).

    For me it seems so, that when activating PL it does anything with the rewrite rules, so that they are not working anymore. In fact “skater” is not set, because the wrong rule is taken. One of the real strange things is, that the rule analyzer is working as expected in the WP-Admin.

    Du you have an idea? It is really important to me, because PL seems to be must more performant then WPML and I really would like to switch. But therefore it has to work before πŸ˜‰

    regards,
    Lars

    Thread Starter fokus3

    (@fokus3)

    I guess I’ve found a work-around. It seems to have anything to do with the check_canonical_url function. Now I used the undocumented filter (see below):

    add_filter('pll_check_canonical_url','canonical_url');
    function canonical_url($redirect_url) {
    	return false;
    }

    It is nice that it works, however I don’t understand, why my rule isn’t fired in the first step.

    Plugin Author Chouby

    (@chouby)

    Could it be that the rule was correctly matched and then you are redirected to the wrong url by check_canonical_url?

    I was making my own tests with Polylang 1.7 beta. I did modify the code of this function to make it more robust. Could you check if this version already fixes your issue?

    Thread Starter fokus3

    (@fokus3)

    I already made the work with this beta (I guess πŸ™‚ Well, I will check this in the next days and it seems so, that it was redirected by this function and so my custom query_vars gone lost. Currently, I don’t understand the system behind, but I will investigate here further…

    Plugin Author Chouby

    (@chouby)

    The goal of this function is to avoid duplicate content and errors 404. It aims to be a complement of the canonical redirection of WordPress. Thanks to you, I noticed that I introduced a regression in the development version which I fixed in the new version (1.7beta4)
    https://downloads.wordpress.org/plugin/polylang.zip

    However, as mentioned above, this will still conflict with your rewrite rule as one page = one langage, so due to the page_id in the rewrite rule, Polylang will always try to redirect to the url corresponding to the page language.

    Thread Starter fokus3

    (@fokus3)

    I will test the new version and hope that it will solve my 404-problem πŸ™‚ However with this beta occurred an error with redeclaration of icl_get_home_url

    PHP Fatal error:  Cannot redeclare icl_get_home_url() (previously declared in /Volumes/Daten/Websites/SpeedSkatingNews.info/wp-content/plugins/polylang/include/wpml-compat.php:44) in /Volumes/Daten/Websites/SpeedSkatingNews.info/wp-content/plugins/sitepress-multilingual-cms/inc/template-functions.php on line 27

    It necessary to have both plugins activated so that the WPML-Importer can work….

    Plugin Author Chouby

    (@chouby)

    It necessary to have both plugins activated so that the WPML-Importer can work….

    I wrote exactly the opposite in the description of WPML to Polylang.

    Thread Starter fokus3

    (@fokus3)

    I get the Fatal Error when I try to activate both plugins at the same time πŸ™

    Plugin Author Chouby

    (@chouby)

    Sure you can’t activate WPML and Polylang at the same time. And for sure, you should never activate 2 multilingual plugins at the same time. There are big risks to obtain very unexpected results.

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘Redirection with custom rules fails’ is closed to new replies.