0

I have the following custom rewrite rules.

add_action( 'init', 'corse_rewrite_rules', 0 );
function corse_rewrite_rules( ) {

    add_rewrite_rule('listings/make/([^/]*)/page/?([0-9]{1,})/?$', 'index.php?post_type=product&product_make_model=$matches[1]&paged=$matches[2]', 'top');

    add_rewrite_rule('listings/make/([^/]*)/?$', 'index.php?post_type=product&product_make_model=$matches[1]', 'top');

}

Which should take a URL like this :

/product_make_model/stuff/

to :

/listings/make/stuff/

The issue in hand is that although the Rewrite is working and the new URL is accessible, its not redirecting the old URL to the new one.

If I also add an SEO plugin such as YOAST the canonical URL is listed as the the old URL confirming that there is something wrong.

Could someone shed some light as to what im doing wrong ?

  • I'm no expert, possibly stating obvious... the first argument of add_rewrite_rule is the 'old' URL to match and the second argument is the place to redirect to. So I'd expect your functions to try to redirect to /product_make_model/stuff/. Is that your intention? – Jon Feb 23 '17 at 13:02
  • The first one is the "new URL" the second one is the "old URL" . I want to redirect to /listings/make/stuff – Randomer11 Feb 23 '17 at 13:20
  • I think they are the wrong way round then, but as I say, not an expert. For the avoidance of doubt I think those rules will mean that someone clicking on a link to /listings/make/stuff/ will be redirected to /product_make_model/stuff/. – Jon Feb 23 '17 at 14:26
  • Unfortunately both URL's work and neither is redirecting, if I remove the rules then the old URL still works and the new one doesnt. So I assume they are the correct way around. I have tested them the other way and no luck :( – Randomer11 Feb 23 '17 at 14:34
  • This is a common misconception regarding adding rewrite rules. They only map incoming requests to query vars. Generating the URLs for content in the correct format or redirecting to the correct format is separate and unrelated to the rewrite rules. – Milo Feb 26 '17 at 16:14
1

Ok managed to figure this out, instead of adding action for a rewrite. Because what I was wanting to rewrite was a taxonomy i should have been using something like this :

'rewrite' => array('slug' => 'listings/make', 'product_make_model' => false)

When registering the taxonomy, which basically rewrites the slug as the taxomomy is created/registered.

Works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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