• I want to add a simple custom WordPress rewrite rule but somehow I don’t get this working.

    This URL
    https://www.gewerbesteuer.de/steuer/muenchen should call this

    https://www.gewerbesteuer.de/index.php?pagename=gewerbesteuerhebesaetze&loc=muenchen.

    So I want to call a page, which displays tax rates for a certain city, the city should be in the url as the last part.

    Here is my code:

    function custom_rewrite_tag() {
        global $wp_rewrite;
        add_rewrite_tag('%loc%', '([^&]+)');
        add_rewrite_rule('steuer/([^/]+)',
            'index.php?pagename=gewerbesteuerhebesaetze&loc=$matches[1]',
            'top');
        flush_rewrite_rules();
    }
    
    function query_vars($query_vars ) {
        $query_vars[] = 'loc';
        return $query_vars;
    }
    
    add_action('init', 'custom_rewrite_tag');
    add_filter( 'query_vars', 'query_vars' );

    The rewrite rule is working but the parameter (in this case loc) is not picked up. Even if I hardcode the rewrite rule with a certain city like

    add_rewrite_rule('steuer/([^/]+)',
        'index.php?pagename=gewerbesteuerhebesaetze&loc=muenchen', 'top');

    it still doesen’t pick up the value of the loc parameter. I also noticed that the $matches array is empty and doesn’t contain any values. I went through all the docs at wordpress and the questions here, but couldn’t find the problem. Any ideas?

    Thanks

    Bernhard

  • The topic ‘WordPress custom rewrite rules not working’ is closed to new replies.