skip to Main Content

WordPress Redirects

My client has a WP website with a very bad permalinks structure and few hundreds “product” pages that they created as simple posts with bunch of HTML.
For example:

http://www.website.com/article/sony-tv-42-inch
http://www.website.com/article/iphone-5-2-black
http://www.website.com/article/samsung-dvd-player-12455

I am creating a new site from scratch and planning to use custom post types for Products section and organize URLS like:

http://www.website.com/product/sony/tv-42-inch
http://www.website.com/product/apple/iphone-5-black
http://www.website.com/product/samsung/dvd-player-12455

Since he doesnt want to lose any traffic or SEO ratings, i was wondering what would be the simplest solution for htaccess redirect for few hundred posts?
If he only had dozen of them, i could do it manually, but few hundreds…
Also, bare in mind that this is clean WP install with a theme built from scratch (i am working locally and most likely will be importing products via CSV file) so i cant just change the permalinks structure on production website.

Any help would be appreciated

2

Answers


  1. For SEO, i suggest you use 301 – redirect in your .htaccess

    You can use regexp for redirect.

    Example:

    RewriteEngine on
    RewriteRule ^/users/(.*)$ http://www.example.com/profiles/$1 [R=301,L]
    
    Login or Signup to reply.
  2. It’d be fairly easy for you to re-write from

    http://www.website.com/product/sony-tv-42-inch
    

    to

    http://www.website.com/product/sony-tv-42-inch
    

    but that extra / is going to present a problem.

    As mentioned by some others you certainly want a 301 for SEO purposes, so here is what I would do.

    Step 1 – Get current URLS:

    Get a list of all current post permalinks. This SQL query for MySQL by David George should do the trick.

       SELECT  wpp.post_title,
            wpp.guid,
            wpp.post_date,
            CONCAT
            (
              wpo_su.option_value,
              REPLACE
              (
                REPLACE
                (
                  REPLACE
                  (
                    REPLACE
                    (
                      wpo.option_value, 
                      '%year%',
                      date_format(wpp.post_date,'%Y')
                    ),
                    '%monthnum%',
                    date_format(wpp.post_date, '%m')
                  ),
                  '%day%',
                  date_format(wpp.post_date, '%d')
                ),
                '%postname%', 
                wpp.post_name
              )
            ) AS permalink
      FROM wp_posts wpp
      JOIN wp_options wpo
        ON wpo.option_name = 'permalink_structure'
       AND wpo.blog_id = 0
      JOIN wp_options wpo_su
        ON wpo_su.option_name = 'siteurl'
       AND wpo_su.blog_id = wpo.blog_id
     WHERE wpp.post_type = 'post'
       AND wpp.post_status = 'publish'
     ORDER BY wpp.post_date DESC 
    

    Step 2 – Save it for later:
    Now that you have that list dump it into an Excel column for later.

    Step 3 – Format it for PHP:
    Take all the URLs and dump them into Notepad++ or equivalent. On windows hit cntrl+H to bring up the find/replace function. Make sure to select ‘Search Mode’ -> ‘Extended’

    For “Find What” you should put in rn and for ‘Replace With’ you should put in , and hit ‘Replace All’.

    You should now have a list of all your URLs separated only by a comma.

    Step 4 – Create a PHP File:

    Create a new PHP file and set:

        <?php
    $urlString = "http://www.urlsample1.com/article/sony-thing-and-stuff, http://www.urlsample1.com/article/warner-brothers-stuff";
    
        //this will replace the word article with the word product
        $newstring = str_replace("article", "product", $urlString);
        //turns string into array
        $myArray = explode(',', $newstring);
    //loops through array to find first case of - and turn it to a /
    for ($i = 0; $i < count($myArray); ++$i) {
           $haystack = $myArray[$i];
           $needle = "-";
           $replace = "/";
           $pos = strpos($haystack, $needle);
    
            if ($pos !== false) {
              $finalstring = substr_replace($haystack, $replace, $pos, strlen($needle));
            }
        //removes common url elements
        $newShort = str_replace("http://www.urlsample1.com", "", $finalstring);
        $oldShort = str_replace("http://www.urlsample1.com/product", "/article", $myArray[$i]);
        //prints out the old and new url in .htaccess format
        print("Redirect 301 " . $oldShort . " " . $newShort . "</br>");
    };
    
    ?>
    

    Step 5 – .Htaccess

    Your output from above should look like:

    Redirect 301 /product/sony-thing-and-stuff /product/sony/thing-and-stuff
    Redirect 301 /product/warner-brothers-stuff /product/warner/brothers-stuff
    

    and can be put into the .htaccess file.

    Note: This method may become slow with Apache and isn’t preferable but it is the quickest way I can think of to get your client up and running on the new situation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search