How to stop permalink redirects for a particular slug

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 Answers
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;
    }
}

Leave a Comment