0

I have an issue with Canonicalization where on Custom Post Types say for example:

/custom-post-type/{post_id}/fake-post-name/

is loading up but not redirecting to :

/custom-post-type/{post_id}/real-post-name/

It basically is allowing anything to be after post id and load that URL instead of redirecting to the correct one.

Ive got my CPTs setup with custom rewrite rules using 'add_rewrite_rule' and associated my custom post links with 'post_type_link'.

Have I missed a step somewhere?

Many thanks.

Just add to this the actual Canonical tag displays the correct url it should redirecting to.

Will I need to add a filter to canonical_redirect and check the post_name of the CPT and see if it matches for the given post id and pass it back perhaps? Im not sure if this is needed/or way to fix it given the canonical in the is correct?

1 Answer 1

0

Think ive solved it and can't believe what it was!

On my rewrite rule I had:

add_rewrite_rule( 
        'custom-post-type/([0-9]+)/([^/]*)/?$',
        'index.php?post_type=customposttype&p=$matches[1]',
        'top'
    );

Adding in and passing the post_name for the custom post type got it to start redirecting.

add_rewrite_rule( 
        'custom-post-type/([0-9]+)/([^/]*)/?$',
        'index.php?post_type=customposttype&p=$matches[1]&customposttype=$matches[2]',
        'top'
    );

Anycase for now its late and it seems to have started working :-) Now at least it forces the 301 from the fake post to the real post as was previously put in canonical meta. Don't know why but thought if the id was provided it would have worked without having to take in the post_name.

Sometimes WP can really have you pulling your hair out!

1
  • Moral of the story from this for me sleeping on it is if you include variables in the url string make sure all are passed in even if you don't believe Wordpress will use them for thoroughness then this should not occur. Hopefully this will help someone else in future.
    – Jimbo
    Commented Mar 16, 2013 at 15:22

Your Answer

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

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