Why am I getting a 301 on a existing post?

This is bizarre…

I have two posts that when I try to view them, they get a 301 and proceed to redirect to the next post in line. These posts are published. I don’t have any plugins messing with redirections. I have tried disabling other plugins that could cause this issue. I have also checked the guid in the database and it’s correct. The htaccess has the basic wp pretty permalink structure. For some reasons this 2 links have been marked as 301, and I can’t see where this was done.

Any ideas?

UPDATE:

This is getting really weird…

So trying to troubleshoot I did the following:

1. Download the site to local dev.
I thought if the problem is the whole link, then if I change it to a dev link, the problem will no longer exist. And that would rule out domain level 301 redirect.

Turns out that the problem still exisits under a different domain name.

2. Disabled all plugins
I disabled all plugins. The problem persisted. Just in case, I deleted all plugins. No luck.

3. Search for hard coded links
I searched for hard coded links in the whole theme. I also searched for any php redirects or js redirects. Nothing.

4. So I resintalled the WP core
I wanted to make sure that had nothing to do with it. Nothing.

5. I deleted all revisions

6. Disabled ACF custom fields plugin
I thought that maybe the redirect is hidden in some custom field. So disabled the plugin that displays them. The theme barely display correctly but the page redirects anyway.

I cannot switch themes, because the whole logic of displaying this page is done in the theme, and uses a lot of custom post types and fields.

So the next option, was to delete the page that the redirect was pointing to. So I for example I have Post 1 redirecting to Post 2. I deleted Post 2 (trashed it). The redirect still took me to the Post 2 page, but of course that now I got a 404 instead. Since Post 2 is in the trash.

So I decided to delete Post 2 from the trash, so that it would no longer be in the database. Once I deleted Post 2… tada! Post 1 was no longer redirecting. That solves the issues, but clearly I cannot delete Post 2 on the live site…

I am still clueless as to what’s going on…

UPDATE 2:

So I wanted to be 100% sure that the problem is not in the files but in the database. So I deleted pretty much everything, and only left what’s need to make the pages in questions load without errors. That means that I deleted everything from functions.php, deleted all templates but the ones affected, delete styles, images… everything. Deleted almost everything from header.php.. leaving only the doctype and style inclusion (that’s where the theme name is). And the redirect still happens. I even removed wp_head, and no luck. So now I know that the problem is in the database.

2 s
2

After pretty much destroying the website (copy) I found the answer, but I am not 100% sure of the cause.

WordPress saves the name of old posts slugs, so that it can create 301 redirects. It adds an entry to the postmeta table called _wp_old_slug. I had over 110 entries on my postmeta table with many different values. There were a whole bunch of posts with the wrong value. I am not sure how this happened, but this was causing the 301 redirects. As soon as I removed these entries from the database the problem was fixed.

Maybe _wp_old_slug functionality broke during one of the updates? And it was fixed it later without anyone noticing? I am not sure, but I had not even heard of this before. I got clues to my solution thanks to this website.

Leave a Comment