• Hi.
    I want to add custom rewrite rules for posts and categories and …
    for example for categories:
    https://site/cat/%5Bcatid%5D
    and for posts:
    https://site/post/%5Bpostid%5D
    the default permalink settings is set and the rewrite rules array is empty.
    i used this code for testing if it works or not :

    <?php
    
    function custom_rewrite_basic() {
      add_rewrite_rule('^post/([0-9]*)/?', 'index.php?p=$matches[1]', 'top');
    }
    add_action('init', 'custom_rewrite_basic');
    ?>

    and then flushed rewrite rules through clicking save in permalink settings in admin.
    but still the array of rewrite rules is empty and there is no changes to permalink.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter th90

    (@th90)

    i tested adding new rules when the custom permalink structure is enabled in admin->settings->permalinks
    in this way adding new rules working but the rule does not changes the links in theme.
    for example the rule for changing category links like this:
    https://site/cat/5
    works when i type this in web browser url bar.
    but the links inside them shows the permalinks structure that is set in admin settings permalinks page.

    the code i used is :

    <?php
    
    function custom_rewrite_basic() {
      add_rewrite_rule('^cat/([0-9]*)/?', 'index.php?cat=$matches[1]', 'top');
    }
    add_action('init', 'custom_rewrite_basic');
    ?>

    Thread Starter th90

    (@th90)

    i dumped the $wp_rewrite and saw that my added custom rules are placed in [“extra_rules_top”]

    => array(1) { [“^cat/([0-9]+)/?”]=> string(25) “index.php?cat=$matches[1]” }

    and the [“rules”] is empty (false)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘add_rewrite_rules not working!’ is closed to new replies.