url rewriting - Rewrite Rules are Redirecting

admin2025-01-07  3

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

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');
}
Share Improve this question asked Mar 20, 2020 at 18:21 J.S.T. AndrewsJ.S.T. Andrews 11 bronze badge 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. – Admiral Noisy Bottom Commented Mar 21, 2020 at 3:26
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256133a337.html

最新回复(0)