Creating 301 Redirects for Post, Page, Category and Image URLs?

Hi @CJN:,

Your first question, moving the WordPress directory is handled differently from the rest.

Moving WordPress from Subdirectory to Root:

Go into /wp-config.php and add the following to defines (using your client’s domain instead of example.com of course):

define('WP_SITEURL', 'http://example.com');
define('WP_HOME',    WP_SITEURL);

301 Redirects using template_loader and wp_safe_redirect()

You can solve most of the rest of your questions by modifying .htaccess as @Kau-Boy shows how, or you can just do it in PHP. WordPress has a template_redirect hook you can use for this along with the wp_safe_redirect() function to redirect with a 301 HTTP status code. As you can see the rest is just brute for PHP code and a bit of regular expression magic sprinkled in. You can put this code practically anywhere in your theme’s functions.php file:

add_action('template_redirect','my_template_redirect');
function my_template_redirect() {
  $redirect_to = false;
  list($url_path,$params) = explode('?',$_SERVER['REQUEST_URI']);
  $path_parts = explode("https://wordpress.stackexchange.com/",trim($url_path,"https://wordpress.stackexchange.com/"));
  switch ($path_parts[0]) {
    case 'products-directory':
      $redirect_to = '/products-top-level-page';
      break;
    case 'about-directory':
      $redirect_to = '/about-top-level-page';
      break;
    case 'services-directory':
      $redirect_to = '/services-top-level-page';
      break;
    default:
      if (preg_match('#same-word(.*)#',$path_parts[0],$match)) {
        $category = str_replace('.html','',$match[1]);
        $redirect_to = "/newcategory/{$category}";
      } else {
        // Do whatever else you need here
      }
  }
  if ($redirect_to) {
    wp_safe_redirect($redirect_to,301);
    exit();
  }
}

Consider Usability and Not Just SEO?

I would ask if you really want to do #2? IMO that makes a site a lot less usable for users than one that is optimized solely for perceived SEO (and as founder and one time co-organizer of this group I’m not an SEO neophyte.) I’d much rather see you just drop the "-directory" from the first segment of the URL path. JMTCW anyway.

Generating 404 Pages?

If you need to issue a 404 you can do it with header:

header("HTTP/1.0 404 Not Found");
exit;

However I think that’s not what you want to do, correct? I think you can achieve whatever redirect logic you need by modifying the PHP function above and responding to the HTTP request with a 301, right?

Importing Images into Media Library and 301 Redirecting

You could move them into the media library and doing so would allow you to manage them moving forward. Here’s a plugin that might help (although I’m not sure if it is working with 3.0; if not it might be a good code base to work with anyway):

Hardcoding Image URL 301 Redirects using an Array

Then since it would be a one-time thing you could simply hardcode your image URLs into an array and use them to match in your redirection function. Modifying the default in the switch statement from the code above it might look like this:

default:
  if (preg_match('#same-word(.*)#',$path_parts[0],$match)) {
    $category = str_replace('.html','',$match[1]);
    $redirect_to = "/newcategory/{$category}";
  } else {
    $images = array(
      '/images/image1.jpg' => '/wp-content/uploads/2010/08/image1.jpg',
      '/images/image2.jpg' => '/wp-content/uploads/2010/08/image2.jpg',
      '/images/image3.jpg' => '/wp-content/uploads/2010/08/image3.jpg',
      );
    if (in_array($url_path,$images)) {
      $redirect_to = $images[$url_path];
    } else  {
      // Do whatever else you need here
    }
  }

Using preg_match() to 301 Redirect Images by URL Pattern

Of course if your image URLs follow a pattern you could streamline much or all of the images array using a preg_match() instead, like so:

if (preg_match('#^/images/(.*)$#',$url_path,$match)) {
  $redirect_to = "/wp-content/uploads/2010/08/{$match[1]}";
}

Hope this helps?

Leave a Comment