• Resolved SpidermanPuddin

    (@spidermanpuddin)


    Trying to create custom rewrite rules from
    /near-me/?region=NY&city=New-York-City
    to
    /near-me/NY/New-York-City/

    It’s not working properly and redirecting to page that WordPress thinks is related.

    //* Add Rewrite Rule
    add_action( 'init', 'prefix_nearme_rewrite_rule' );
    function prefix_nearme_rewrite_rule() {
        add_rewrite_rule( 'near-me/([^/]+)/([^/]+)/', 'index.php?region=$matches[1]&city=$matches[2]', 'top' );
    }
    
    //* Register Variables
    add_filter( 'query_vars', 'prefix_register_query_var' );
    function prefix_register_query_var( $vars ) {
        $vars[] = 'region';
        $vars[] = 'city';
    
        return $vars;
    }
    
    //* Rewrite Template
    add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
    function prefix_url_rewrite_templates() {
    
        if ( get_query_var( 'city' ) && get_query_var( 'region' ) && is_singular( 'gun-shows-near-me' ) ) {
            add_filter( 'template_include', function() {
                return get_template_directory() . '/page_near-me.php';
            });
        }
    
    }
    • This topic was modified 6 years, 3 months ago by SpidermanPuddin. Reason: clarifying
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    WP is still matching the page name rewrite rule before it gets to your rule. The ‘top’ designation still is below built-in rewrite rules. The simplest measure is to rename the page slug so the page name rewrite rule fails to find a match and matching can proceed to the proper rewrite rule.

    Thread Starter SpidermanPuddin

    (@spidermanpuddin)

    The slug is important. What’s the alternative?

    Moderator bcworkz

    (@bcworkz)

    What would distinguish a page request from a “near-me” request? Maybe that could be looked at in pre_get_posts action and the query vars altered accordingly.

    Thread Starter SpidermanPuddin

    (@spidermanpuddin)

    I was able to find this code but it’s still not working even after flushing the permalinks.

    //* Add Rewrite Rule
    add_action('init', 'sym_nearme_rewrite_rule', 10, 0);
    function sym_nearme_rewrite_rule() {
        add_rewrite_rule('^near-me/([^/]*)/([^/]*)/?','index.php?page_id=4331&region=$matches[1]&city=$matches[2]','top');
    }
    Moderator bcworkz

    (@bcworkz)

    What is the page’s permalink path (you may omit the domain portion) that is conflicting?

    Would you consider adding a couple numbers like /0/1/ to the rewrite path? This causes WP to include a date parameter in a normal page query, which normally results in a page not found. The thought being that it’ll then match the proper rewrite rule when that happens. Just an untested thought.

    Otherwise I think you need further logic outside of rewrite rules, either in “request” or “pre_get_posts”.

    Thread Starter SpidermanPuddin

    (@spidermanpuddin)

    Found the solution. The Rewrite Rule was correct way to go, but also needed to 1) Add the (Unique) Query Variable so WordPress would recognize it 2) Use the variable instead of GET.

    add_action( 'init', 'nearme_rewrite_rules');
    function nearme_rewrite_rules() {
        add_rewrite_rule('^near-me/([^/]*)/([^/]*)/?','index.php?page_id=43316&nmregion=$matches[1]&nmcity=$matches[2]','top');
    }
    
    add_filter('query_vars', 'nearme_var', 0, 1);
    function nearme_var($vars){
        $vars[] = 'nmregion';
        $vars[] = 'nmcity';
        return $vars;
    }
    
    $region = get_query_var('nmregion');
    $city = get_query_var('nmcity');
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom Page Rewrite Rules’ is closed to new replies.