3

I have a large number of posts that each represent a very specific type of data. Each post title is the SKU for the data & information. Folks have become pretty savvy on the URL structure and their shortcuts (redirects) are causing them to get incorrect data. Using the wrong data is NOT a good thing in this case.

For example, if my actual slug is example.com/esm/010HHHH

  • example.com/010HHHH redirects to example.com/esm/010HHHH (Good)
  • example.com/010 could redirect to any item that starts with 010 such as example.com/esm/010AAAA (Not Good)

The preferred behavior is to simply get a 404 error for any post that is not exact for any posts that have a slug of /esm.

I have successfully solved the issue by removing the canonical filter. However, I would like the "normal" canonical behavior for any other slugs.

remove_filter('template_redirect', 'redirect_canonical');
2
  • How would you differentiate a slug that should have redirect_canonical applied from one that shouldn't?
    – Milo
    Commented Dec 15, 2014 at 22:14
  • Any post that has the slug "/esm" should not have it applied, all others should.
    – Eddie
    Commented Dec 15, 2014 at 22:32

2 Answers 2

2

maybe this will help :

add_filter( 'redirect_canonical','custom_disable_redirect_canonical' ); 
function custom_disable_redirect_canonical( $redirect_url, $requested_url ) {
    if ( /* your condition */ ) {
        return $redirect_url;
    } else {
        return $requested_url;
    }
}
0
2

This seems to be working. If the URL that WP wants to redirect to has "esm" in the url, it will simply return a 404. If not, it can go on its merry little way.

add_filter( 'redirect_canonical','custom_disable_redirect_canonical' );
function custom_disable_redirect_canonical( $redirect_url, $requested_url ) {
  if ( preg_match("/esm/",$redirect_url) ) {
    return FALSE;
  } else {
    return $redirect_url;
  }
}

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.