When I trimmed down my categories in WordPress I paid special attention to make sure that all my new 404 errors would be redirected to a logical place because of the restructure. That’s fine since there are only about 19 re-directs, but after removing 100 tags I decided there had to be a better way to handle these missing tags than an additional 100 redirects.
I decided my search tool had to be string enough to handle it for me, and why shouldn’t my 404 page just immediately re-direct my users right to the search. One less click for them!
So below my redirects on my 404 page, but above the WordPress template tag “get_header()” I put in this snippet:
$request = $_SERVER['REQUEST_URI']; // This line was implemented in the 404 redirect fix
$tag_base = get_option('tag_base');
if(empty($tag_base)) $tag_base = 'tag';
$pattern = '/\/('.$tag_base.')\/([a-z0-9-]*)\//i';
if(preg_match($pattern, $request, $matches)){
$search_for = str_replace('-', ' ', $matches[2]);
$search_for = urlencode(trim($search_for));
//Content hasn't moved but for our purposes this 301 header might be more useful than a 404
header("HTTP/1.1 301 Moved Permanently");
header('Location: /?s='.$search_for);
die();
}
Now if I requested the page /tag/boston/, since I no longer have tags by the name “boston” it would redirect to a search for “boston” just like the search box does.
Since the main purpose of this modification is to logically replace old tags I also took into consideration that WordPress hyphenates multi-word tags, so I added a string replacement to replace the hyphen with a space. So requesting /tag/flickr-explore/ would search for the query term “flickr explore” instead of “flickr-explore” which would yield no results.
No perfect but a great quick and dirty solution!
Update it even now accounts for any change you’ve made to the tag base.
So if in the WordPress Options>Permalinks you changed the tag base to be ‘/tagging/‘ or ‘/folksonomy/‘ instead of the default ‘/tag/‘ then it will account for that changing without you having to update my snippet. Ain’t I so smart :)
This site runs on the Thesis WordPress Theme
If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.
If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:










{ 1 comment… read it below or add one }
Hi, but I noticed both of your tag redirection examples end up at non existing pages (return status of 404)
I tried the link embedded in your post as well as the url without the /tag/
a-la:
blog.ninedays.org/tag/boston — 404
blog.ninedays.org/boston — 404
Have you done some more work on this?