11

On one of my pages I changed the slug to form a different URL. E.g.

Old: http://example.com/old-slug

New: http://example.com/new-slug

WordPress has done it's thing of redirecting http://example.com/old-slug to http://example.com/new-slug.

I'd like to remove this behaviour as a plugin I am using makes use of the slug in question and the redirect overrides its behaviour.

I checked this question, and checked my wp_postmeta table for instances of _wp_old_slug but nothing is returned. My server is Nginx so shouldn't be affected by .htaccess files.

Is there anything else I can do to remove this redirect?

1
  • 2
    It seems odd that your wp_postmeta table wouldn't have any _wp_old_slug keys - the bit of code that does that is in wp-includes/query.php (wp_old_slug_redirect()) - you could add an exit or debug statement there to check if it's being called. Also, remember that if WordPress can't find a permalink, it looks posts which match the start, e.g. if you had a post called /foobar, then /foo will redirect to it. Commented Apr 8, 2015 at 14:11

5 Answers 5

8

This (in your functions.php) will turn it off (but see also the comment I've left):

remove_action('template_redirect', 'wp_old_slug_redirect'); 

It seems odd that your wp_postmeta table wouldn't have any _wp_old_slug keys - the bit of code that does that is in wp-includes/query.php (wp_old_slug_redirect()) - you could add an exit or debug statement there to check if it's being called.

Also, remember that if WordPress can't find a permalink, it looks for posts with a matching beginning, e.g. if you had a post with permalink /foobar, then /foo will redirect to it.

3
  • 1
    Could you add your comment into the answer just to flesh it out and have all the content in one place? Commented Apr 8, 2015 at 14:16
  • Indeed, your comment was the solution. I have a page called Events which had the slug events-page. No page had the slug events but when visiting example.com/events it redirected to the Events page. WP was obviously matching the slug to a page with the same title as nothing else existed for it. Commented Apr 8, 2015 at 15:21
  • 1
    didnt work for me Commented Apr 2, 2019 at 17:15
3

this worked for me:

 remove_filter('template_redirect', 'redirect_canonical'); 

source: http://biostall.com/prevent-wordpress-redirecting-to-nearest-matching-url/

2
  • didnt work for me Commented Apr 2, 2019 at 17:15
  • 1
    Astounding that WP would have this enabled by default Commented Feb 23, 2020 at 22:40
1

Every WordPress post has its own slug, which is automatically generated by your post’s title. If you decide to change the post slug later, WordPress will remember the old one and redirect it to the new one. It’s possible to prevent old post slug redirection in WordPress by adding removing a couple of actions from your WordPress core with a small snippet.

Just add following code to your current theme’s functions.php file to prevent WordPress from redirecting old post slugs to new ones:

remove_action( 'template_redirect', 'wp_old_slug_redirect'); remove_action( 'post_updated', 'wp_check_for_changed_slugs', 12, 3 ); 
2
  • how does it affect SEO? Commented Apr 2, 2019 at 16:54
  • I added these lines, It doesnt work Commented Apr 2, 2019 at 16:58
1

To manually remove automatic redirects after slug change, just delete the corresponding rows from the "wp-redirection-items" from the database using phpMyAdmin.

This is the best and simplest way which allows you to remove redirects for specific posts.

3
  • How do you know the OP is using exactly that plugin? Commented Mar 2, 2017 at 13:57
  • 1
    your link doesnt work Commented Apr 2, 2019 at 17:16
  • There doesn't seem to be a table named wp-redirection-items Commented May 20, 2024 at 12:23
0

What helped for was permalinks reset. just go to Settings -> Permalinks, pick default, hit Save Changes. Then pick your structure and hit Save Changes again.

2
  • 2
    doesnt workkkkk Commented Apr 2, 2019 at 17:13
  • This did the trick for me! Thanks. Commented Apr 4, 2022 at 19:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.