by Chris Coyier & Jeff Starr

Redirect Dead-End Category, Search and Tag URLs

Posted by Updated on

This is an exclusive guest post by , posted on June 8th, 2009.

Beginning with version 2.5, WordPress automatically handles many types of canonical redirects. A good example of this may be seen by typing your blog address into your browser both with and without the www prefix. If you are using WordPress 2.5 or better, one of these versions of your blog URL will be immediately redirected to the other. The same type of automatic redirect may be seen for several other non-canonical URL variations, and is handled via PHP deep in the WordPress core.

As good as WordPress happens to be at catching and redirecting non-canonical requests, there are other types of problem URLs that need to be fixed. Specifically, for permalink-enabled WordPress sites, the following three URLs result in dead-end, 404 (Page Not Found) errors:

  • http://your-domain.tld/blog/tag/
  • http://your-domain.tld/blog/search/
  • http://your-domain.tld/blog/category/

The pages referred to by these URLs simply do not exist by default. Although these pages may not be requested by the average visitor, they are frequently visited by search engines such as Google. Whenever Google (or a curious visitor) attempts to load these pages, it encounters a dreaded 404 error. This is not good for your visitors and statistics, and it certainly isn't helping your site's search engine position and page rank. Fortunately, an easy fix is available for those on Apache servers with access to either their httpd.conf or root HTAccess file.

If your site is located in the root directory of your domain, place this code into your root HTAccess file:

RedirectMatch 301 ^/tag/$      http://your-domain.com/
RedirectMatch 301 ^/search/$   http://your-domain.com/
RedirectMatch 301 ^/category/$ http://your-domain.com/

Likewise, if your site is located in a subdirectory called "blog", use this code instead, and place it into your site's root HTAccess file:

RedirectMatch 301 ^/blog/tag/$      http://your-domain.com/
RedirectMatch 301 ^/blog/search/$   http://your-domain.com/
RedirectMatch 301 ^/blog/category/$ http://your-domain.com/

Almost done! If you are using the first block of code (WordPress installed in site root), edit each of the three instances of "http://your-domain.com/" to reflect the location to which you would like to redirect the dead-end URLs. For example, you may want to redirect all three URLs to your home page to extract any leftover rank juice. Another option would be to redirect each URL to a similar page on your site. Anything is possible here; proceed according to your own strategy.

If you are using the second block of code, in addition to editing each instance of "http://your-domain.com/", you will also need to edit each instance of "/blog/" to match that of your actual subdirectory.

After editing the code and uploading to your site, you should now enjoy three less dead-end, 404 errors to worry about, while enjoying the SEO benefits of a cleaner, tighter WordPress-powered blog.

12 Responses

  1. Robert Fischer June 16, 2009

    There are plugins out there which handle all 404s gracefully by redirecting to the home page and adding an error message saying that the URL was not found. One I saw (don’t remember which it was at this point) redirected to a search page after extracting out some keywords. So doing this stuff manually doesn’t really seem worth it.

    • Chris Coyier

      Yeah handling it with .htaccess is a step up the chain from handling it with PHP so it’s a bit cleaner.

      The concept of redirecting a 404 back to the home with a message is interesting though… I’ve never thought of that. Maybe even parsing the URL for keywords and redirecting to a search page for those keywords?

    • Jeff Starr

      I think it all depends on personal preference. Using a plugin is certainly convenient, but doing so requires more server resources and can slow things down. Conversely, adding a few slices of code to your HTAccess file keeps the redirects happening at the Apache level and thus avoids the need for PHP/MySQL to do anything at all, which is better for performance.

  2. I’ve got a follow-up question to this post:

    Let’s say you’ve used a blog in another folder (for instance /blog/) for a while, then you’d move it up to root-level.

    How can you redirect users who links to old URLs (for instance /blog/post-url), to the new one (root-level/post-url)? Is it possible to do this with .htaccess?

    • Jeff Starr

      Hi koew, you should be able to use Apache’s RedirectMatch directive to do this. Try something like this in your site’s web-accessible root .htaccess file:

      RedirectMatch ^/blog/(.*)$ http://domain.tld/$1

      ..and edit the subdirectory and target URL to suit your needs. Test thoroughly :)

  3. Daniel15 June 23, 2009

    Made a mistake in my Lighttpd code, it should be something like:

    url.redirect = ( "^/blog/tag/$" => "http://your-domain.com/",
             "^/blog/search/$" => "http://your-domain.com/",
             "^/blog/category/$" => "http://your-domain.com/")

  4. Daniel15 June 23, 2009

    Just a minor thing, the files are referred to as “.htaccess files” (lowercase with a dot at the front) or “distributed configuration files”, not “HTAccess”. Also, something like this might work for lighttpd:

    url.redirect = ( "^/blog/tag/$" => "http://your-domain.com/",
             "^/blog/search/$" => "http://your-domain.com/",
             "^/blog/category/$" => "http://your-domain.com/",

    Not tested, though (as I don’t have any live blogs powered by lighttpd right now). You can set url.redirect-code to 301 to make a 301 redirect, or wrap this in a check of $HTTP["host"] to make it only apply to your blog vhost. Take a look at the Lighttpd mod_rewrite docs for more details.

    Great blog you have here! I’ll definitely be subscribing to the RSS feed :D

  5. Danny Ashton July 25, 2009

    Hi, thanks for your informative article. I have one question that affects my blog.

    I want to 301 redirect my old category page: www.website.com/key-word/ to www.website.com/category/key-word

    But i find if i use a 301 redirect rule in my .htaccess it redirects all old pages www.website.com/key-word/specific-page – which i want to redirect elsewhere.

    Is there a way to only redirect the www.website.com/key-word/ and not the www.website.com/key-word/specific-pages?

    Sorry if its not very clear, i am very new to the world of .htaccess.

  6. Danny Ashton July 25, 2009

    Hi again…

    I have think i have found a solution – thanks to your blog post.

    I have now added the following:

    redirect 301 /category/ http://www.website.com/category/keyword/
    redirect 301 /category/$ http://www.website.com/category/keyword/

    This redirects the specific category page…and also redirects the pages within the directory in the right way.

    • Jeff Starr

      Awesome, Danny — glad to hear you got it sorted :)

      Don’t forget to capitalize “Redirect” to ensure proper syntax and forward compatibility ;)

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry