admin管理员组

文章数量:1410731

I want to want to make url friendly urls for a custom function I am using in my website. I have added a custom template on my theme dir.

The url rewrite function that looks like this:

function starts_rewrite_rule() {
    add_rewrite_rule('^cities-starting-with-([^/]*)/?','index.php?page_id=340&start_letter=$matches[1]','top');
}
add_action('init', 'starts_rewrite_rule', 10, 0);

The url becomes:

site/cities-starting-with-a/

All good and the parameters are read properly from the function. And when the accessed permalink changes to

site/cities-starting-with-pa/

the sql query changes to "ab".

After I update the Permalinks ( click save on permalinks ). I wanted to extend the functionality and add another url rewrite rule. I added a higher priority because the url patter was identical (except the new url reweire rule had an extra parameter. SO I copy paste the same function and add the extra parameter.

function x_letter_cities_starts_rewrite_rule() {
    add_rewrite_rule('^([^/]*)-letter-words-starting-with-([^/]*)/?','index.php?page_id=340&length=$matches[1]&start_letter=$matches[2]','top');
}
add_action('init', 'x_letter_cities_starts_rewrite_rule', 14, 0);

The url is supposed to become: site/7-number-cities-starting-with-pa/

But it seems like wordpresses rewrite function cant properly detect the extra parameter (7),. so the results are the same as the url that is accessed like:site/cities-starting-with-pa/

It seems like the priority is not working correctly. OR url rewrite rule IGNORES the first part "7-number-" and only reads the second part pattern of the url "cities-starting-with-pa/", because they end in the same pattern.

Can anyone help me in this tricky situation. I appreciate your help/clues/ideas.

Thank you, shoku i petrit.

I want to want to make url friendly urls for a custom function I am using in my website. I have added a custom template on my theme dir.

The url rewrite function that looks like this:

function starts_rewrite_rule() {
    add_rewrite_rule('^cities-starting-with-([^/]*)/?','index.php?page_id=340&start_letter=$matches[1]','top');
}
add_action('init', 'starts_rewrite_rule', 10, 0);

The url becomes:

site/cities-starting-with-a/

All good and the parameters are read properly from the function. And when the accessed permalink changes to

site/cities-starting-with-pa/

the sql query changes to "ab".

After I update the Permalinks ( click save on permalinks ). I wanted to extend the functionality and add another url rewrite rule. I added a higher priority because the url patter was identical (except the new url reweire rule had an extra parameter. SO I copy paste the same function and add the extra parameter.

function x_letter_cities_starts_rewrite_rule() {
    add_rewrite_rule('^([^/]*)-letter-words-starting-with-([^/]*)/?','index.php?page_id=340&length=$matches[1]&start_letter=$matches[2]','top');
}
add_action('init', 'x_letter_cities_starts_rewrite_rule', 14, 0);

The url is supposed to become: site/7-number-cities-starting-with-pa/

But it seems like wordpresses rewrite function cant properly detect the extra parameter (7),. so the results are the same as the url that is accessed like:site/cities-starting-with-pa/

It seems like the priority is not working correctly. OR url rewrite rule IGNORES the first part "7-number-" and only reads the second part pattern of the url "cities-starting-with-pa/", because they end in the same pattern.

Can anyone help me in this tricky situation. I appreciate your help/clues/ideas.

Thank you, shoku i petrit.

Share Improve this question edited Dec 11, 2019 at 14:16 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Apr 27, 2019 at 13:25 Born vs. MeBorn vs. Me 135 bronze badges 2
  • Did you flush your rewrite rules already? – Robbert Commented Apr 27, 2019 at 13:35
  • I flushed. I saved Changes on Settings> Permalinks. – Born vs. Me Commented Apr 27, 2019 at 14:00
Add a comment  | 

1 Answer 1

Reset to default 0

You are trying to match 7-number-cities-starting-with-pa URL to ^([^/]*)-letter-words-starting-with- pattern in second rule.

These rules should work:

add_action( 'init' , 'se336474_rewrite_rules', 5 );
add_filter( 'query_vars', 'se336474_query_vars' );

function se336474_rewrite_rules()
{
    // single pattern for URLs: 
    //    site/cities-starting-with-pa/      => start_letter='pa'
    //    site/7-cities-starting-with-pi/    => start_letter='pi', length=7
    add_rewrite_rule( '^(?:([0-9]+)-)?cities-starting-with-([^/]*)/?$', 'index.php?page_id=340&start_letter=$matches[2]&length=$matches[1]', 'top' );

    //
    // OR for URLs:
    //    site/cities-starting-with-pa/         
    //    site/7-number-cities-starting-with-pi/
    //
    //add_rewrite_rule( '^cities-starting-with-([^/]*)/?', 'index.php?page_id=340&start_letter=$matches[1]', 'top');
    //add_rewrite_rule( '^([0-9]+)-number-cities-starting-with-([^/]*)/?', 'index.php?page_id=340&start_letter=$matches[2]&length=$matches[1]', 'top');
}
function se336474_query_vars( $vars )
{
    $vars[] = "start_letter";
    $vars[] = "length";
    return $vars;
}

You can use page_id=340 only if 340 is a page type post, in other cases a redirection to canonical URL will be made.

About first rewrite rule syntax [ (?:([0-9]+)-)? ] you can read here.

本文标签: custom post typesaddrewriterule wordpress ignoring url patternwp rewrite not working