0

Trying to get a custom rewrite going for a page. Page Slug: /near-me/ Create Redirect: /near-me/STATE/city/ 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');
}
1
  • I'm not an expert on rewriting rules but I'd consider putting rewrites into your .htaccess file rather than relying on Wordpress functions. It's also possible your .htaccess is overriding any customer Wordpress rules. Mar 21, 2020 at 3:26

1 Answer 1

0

Because WordPress use its own rewrite system with regular expression, so that its rewrite rule when you turned on the permalink, it is just index.php.

When it is turned on. It will scan your url and put into parameters. If you do them in .htaccess rewrite, sometimes you will collide in some situations so you need to test thoroughly.

The reason that it is not working because WordPress have made a publicly queryable list. If you parameter does not in their list, it will be ignored. So to make your rewrite working. You may try the following:

add_filter('query_vars', 'q361110_add_custom_queryvars' );
function q361110_add_custom_queryvars( $qvars ) {
    $qvars[] = 'region';
    $qvars[] = 'city';
    return $qvars;
}

//* 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');
}

After that, it should works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.