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
For SEO, i suggest you use 301 – redirect in your .htaccess
You can use regexp for redirect.
Example:
It’d be fairly easy for you to re-write from
to
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.
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:
Step 5 – .Htaccess
Your output from above should look like:
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.