wp_old_slug and Fighting WordPress Canonical URL Redirect
December 15, 2011
I was updating a clients website tonight with a new design and since the content and page layout was essentially completely different, I set it up with a new database. Of course, a webmaster would be remiss to not 301 redirect all of the old pages, so I set about creating redirects for all the other pages.
However, I ran into a couple of issues, due to the way WordPress redirects certain URLs. One issue was with Canonical URL Redirects.
What are WordPress Canonical URL Redirects
To help avoid duplicate content, WordPress redirects a number of urls to a single url, which would otherwise show duplicate content. For example, take a page with a url of ‘/page-name/’, going to ‘?page_id=5’ will redirect to the correct URL.
The redirect process also redirects incorrect urls. So, if you typed ‘/asdf/page-name/,’ WordPress would redirect to the correct ‘/page-name/’ url. The permalink structure you specify under settings is given precedence.
I think they started doing this early in WordPress Version 2 something.
Fighting the WordPress Redirects
Generally this type of redirection is handy and a good idea, significantly reducing the amount of duplicate content, which was a big problem early on with WordPress. However, it can be a pain too!
In this case, I was trying to redirect a page, lets say ‘/old-parent/child-page/’, to a new page. Unfortunately, on the new website, I had created a completely unrelated page with a title of ‘child-page’. As a result, no matter what I tried, the old page would be redirected to the new ‘/child-page/’ and NOT where I actually wanted it.
Since Canonical URL Redirects can be quite useful, I did not want to disable them completely, even though the redirect was ‘broken’ in this case. And, I did not want to add any extra code to do a check for certain URIs, hack around in the redirect module, or mess with the WordPress Htaccess rewrite rule too much.
So, naturally, I decided to just change the new page’s url. However, this is where I ran into my second WordPress redirect issue.
In addition to the redirects mentioned above, WordPress also redirects old page urls. So, if you change ‘child-page’ to ‘new-name,’ WordPress will keep track of this and redirect to the new url. This, again, is handy to ensure you do not break any external/internal links, but it also can be a pain!
The old slug(url) is stored in the postmeta table as a meta_key of ‘wp_old_slug’.
To fix this redirect, I deleted all rows with a meta_key = ‘wp_old_slug’ . Of course, I knew that this was a brand new database and that any old slugs were unneeded. In practice, you would really only want to delete a single entry at a time.
To view all old slug redirects in Mysql, you can use the following query. “wp_postmeta” uses the standard “wp_” table prefix:
select * from wp_postmeta where meta_key="wp_postmeta";
Then, you can remove the old slug(s) as needed.
I also ran into a caching issue, which appeared to have to do with how Google handles an SSL search, but I need to do some more playing with that one…
For Those That Want to Disable it Completely or perhaps wrap it in an if/else, the following disables Canonical URL Redirect and can be placed in your themes functions.php file: