Redirect Old Slugs

About

If you are using “fancy permalinks” in WordPress, part of that permalink will be a “slug,” an all-lowercase dashed version of your post title… like this-is-a-slug. This becomes part of your permalink… and so it cannot change. But what if you decide you want to change it? What if you made a spelling error in your title, and don’t want the permalink’s slug to remain misspelled for all of eternity? You could either just change the slug, and have old links to the post break, or you could go through the trouble of setting up an Apache redirect for the old link to the new one.

This plugin was created to fill that need. Now you can change your post or page slugs with impunity. The plugin will notice when you change the slug of a published post or page, and it will save the old slug as a custom field (the key is old_slug). Now, when someone tries to go to the old post, the plugin will say “hey, that looks like one of the old slugs I saved, let me redirect you to the new location.” Thus, none of the links made to the old permalink are broken. You can change the slug multiple times… and all the old versions will forward to the current link.

Redirect Old Slugs has been tested on WordPress 1.5.2 and the in-development WordPress 2.0.

Note

This plugin’s functionality has been added to WordPress core as of version 2.1, so please deactivate the plugin if you have WordPress version 2.1 or higher!

Download

Instructions

  • Download and unzip
  • Upload redirect-old-slugs.php to your /wp-content/plugins/ directory.
  • Activate it from within the WordPress admin interface.
  • From now on, you can edit slugs without worry! If you have an old post whose slug you changed (prior to installing the plugin), you can manually add an old_slug Custom Field for that entry, and it will be forwarded.

Versions

  • 0.3 Plays better with other plugins, creates post_meta entries more intelligently
  • 0.2 Added support for MySQL < 4.1
  • 0.1 Initial release

I’ve spent a lot of time writing this code and supporting it. If you derive some benefit from this code, please consider showing your gratitute in a more tangible
way. Thanks!

I have received $5.00 this month for my coding efforts.

Donate with PayPalAmazon.com Wishlist


135 Responses to “Redirect Old Slugs”

Comment pages: [1] 2 »

  1. Michael Heilemann |

    You my hero of the day. Give yourself a pat of confidence on the back :)

  2. ZEO |

    yay! my old b2 migration nightmare is over.

  3. Thomas |

    Great idea! Installed it but got these error messages. Any ideas?

    Warning: Cannot modify header information - headers already sent by (output started at /homepages/21/d90712089/htdocs/wordpress/wp-content/plugins/redirect-old-slugs.php:96) in /homepages/21/d90712089/htdocs/wordpress/wp-admin/admin.php on line 10

    Warning: Cannot modify header information - headers already sent by (output started at /homepages/21/d90712089/htdocs/wordpress/wp-content/plugins/redirect-old-slugs.php:96) in /homepages/21/d90712089/htdocs/wordpress/wp-admin/admin.php on line 11

    Warning: Cannot modify header information - headers already sent by (output started at /homepages/21/d90712089/htdocs/wordpress/wp-content/plugins/redirect-old-slugs.php:96) in /homepages/21/d90712089/htdocs/wordpress/wp-admin/admin.php on line 12

    Warning: Cannot modify header information - headers already sent by (output started at /homepages/21/d90712089/htdocs/wordpress/wp-content/plugins/redirect-old-slugs.php:96) in /homepages/21/d90712089/htdocs/wordpress/wp-admin/admin.php on line 13

    Thank you

    Thomas Herold

  4. CT |

    Brilliant idea! Do you think it will work with WP 1.5.1.3?

  5. Mark (post author) |

    Thomas,
    You probably copy-pasted the code instead of using the download link, or uploaded using BINARY. That error means you have a stray space at the end of the plugin file, after ?>

    CT,
    It works with WP 1.5.2, which isn’t that much different from 1.5.1.3, although you should probably update to 1.5.2 for security reasons.

  6. CT |

    Good to hear. Now if only it could convert my old BlogSpot hard-coded links…

  7. Ozh |

    Great. This has been in my todo/wishlist for ages, but the “concept” I was thinking about was far from being as simple and efficient than yours.

  8. Geof F. Morris |

    Yay, Mark! I will definitely check this out and support you if I use it.

  9. Trevor |

    Absolutely great idea. I think 1.6 is supposed to push all rewrite rules through index.php (handle permalinks internally) - will this plugin work that way?

  10. O.F. Jay |

    Any luck this can be adopted for reslugged categories? As you can see my question got no replies from the forums.

  11. Mark (post author) |

    Trevor,
    Yes, I developed the plugin using WP 1.6 and then made alterations so that it would also work with WP 1.5.x, and tested successfully on both!

    Jay,
    There is no clean way to do this with categories, but your redirect method will work with the alterations I suggested on the thread.

  12. Bryan Veloso |

    I love you. Now can you get this to work so I can completely remove the /archives/ part of my URI?

  13. Mark |

    Bryan,
    One line in .htaccess should do the trick (.htaccess in your root directory):

    Redirect 301 /archives/ http://yourwebsite/

    From the sound of it, people are looking for all sorts of redirection. Some of this is tricky to do in WP 1.5.x, but will be really easy in WP 1.6, because mod_rewrite passes everything on to WP’s internal rewrite engine, so when the public alpha of WP 1.6 rolls around, I may work on a more versitile redirection package.

  14. Bryan Veloso |

    Thanks Mark, really appreciate the help. So is that “Redirect 301 /archives/” all I need? Because it looks like the comment is a little foobared after that.

  15. Mark (post author) |

    bah, my auto hyperlink code ate it… it should be the front page of your website, followed with a slash.

  16. Robert |

    Mark—-+
    Something appears to be amiss;~/ Using the appropriate entry (topmost at the moment) over at Welcome to the St Buryan FC blog. You can see the error that results. Did I interprete the instructions incorrectly? I am using Permalinks. Also I use the Opt-in Front Page plugin… which may be the reason for this issue. Grateful for your advice to remedy;~)
    +—-best wishes, Robert

  17. Mark (post author) |

    Robert,
    Can’t figure out what’s wrong there, it looks fine. It could be that other plugin. Does it work on other posts, that aren’t touched by the plugin? Does it work if you disable the plugin? What version of PHP and MySQL are you running?

  18. Robert |

    Mark—+
    I am slightly tied up with my options. My use of the Opt-in Front Page plugin was to resolve the infernal inability of searchbots to correctly display the blog’s front page. My use of Apache/mod_rewrite and Permalinks was to enable proper search engine compliant archives. But I make mistakes like everyone else and I also change my mind as to how best to show things… So, when I saw your redirect-old-slugs plug-in I was really quite pleased;~) However I have not been able to get your plug-in to function as I expected or perhaps how you have designed it. Both of my sites error similarly;~/ To remove the front page plug-in would be counter-productive (once again). I am not a programmer per se but otherwise reasonably proficient.
    * SME Server v6.0.1-01 (RH7.3 derivative)
    * PHP 4.3.8
    * MySQL 3.23.58
    * PHPA 1.3.3r2
    * much use of .htaccess
    * WP 1.5.2
    Things errorsome seem to revolve around the date… Would my sites’ use of dates in the format of yyyy-mm-dd be of any negative significance? The server is mine and situated here ie it is not remotely hosted.
    +—-best wishes, Robert

  19. Robert |

    Mark—-+
    Forgot to answer one of your questions: on the other site my attempts with a *non-front page* posting failed similarly as that with the initial site’s front page. Both sites run with a pretty much similar plug-in composition but with radically different content/style.
    +—-best wishes, Robert

  20. Mark |

    Ah! Figured it out. Thanks for your server info, that helped. You have an older version of MySQL which does not support DAY(). Still, your version is supported by WordPress, so I should have used the DAYOFMONTH() function for greater compatability. I’ve uploaded a new version with that fix.

  21. Robert |

    Mark—+
    That’s the one, my thanks.
    What with a date problem and Tempus Fujit
    it seems things are more like Tempus Errata…
    I’ll get my coat;~)
    +—-best wishes, Robert

  22. Uninen |

    This is a brilliant plugin. This should come preinstalled with every WP install. Great work! :)

  23. Max |

    Thank you very much.

  24. Thomas |

    I just had a case with a misspelling and I changed the slug but it does not redirect. I get an 404 message now. Any ideas?

  25. Mark (post author) |

    Thomas,
    Can you provide me with the old URI, the new URI, and your version of WordPress? And just to be clear, you must have the plugin enabled at the time that you change the slug in order for it to notice! It can only keep track of changes since being activated.

  26. Mark (post author) |

    Version 0.3 is out… mandatory upgrade! I identified a few instances where the plugin might interfere with other plugins, by not returning a post ID properly, and also noticed that it was improperly adding old_slug entries that matched the current slug (harmless, but unnecessary).

  27. McNicks |

    Is there any way to get the slug redirection working for pages as well as posts?

  28. Tassoman |

    I would ask the same: how to get it working also for pages?

    Maybe we only need to edit .htaccess by hand?

  29. she |

    hi. is there any way to remain permslink if i change the permalink in admin &gt option &gt permlink ?

    i don’t write in english in title, so address is awful when seeing. so i want to change the permlink to id number from title. but i can know about the way to solve.

  30. she |

    oh.. i found the plugin. thanks, anyway,

  31. Dayna |

    Is it able to work in pages?

    For example, my old URL is

    http://siteurl.com/1/2

    my new URL is

    http://siteurl.com/1/a/2

    will it be able to work?

    thanks :)

  32. krista- the silent k |

    Hi,

    What a great plug in! Currently I use the default permalink structure. If I install this plug in can I switch to fancy permalinks, and the plug in will redirect any links to the old default permalink structured posts?

  33. Mark (post author) |

    Krista,
    For that, you should use Scott Yang’s Permalink Redirect.

  34. eduardo |

    Hey Mark,
    Very useful, thanks. But i might have bumped into a bug or incompatibiliby, which was reported on comment #36 (but the use failed to get back to you with more details).
    Using 1.5.3b1 here. The plugin was activated, but when i changed the post slug it seemed not to work. Then i went and manualy created the custom field “old_slug” with the value matching the old slug (of course), which the plugin had failed to create, and the redirection works.

  35. Nate |

    Brilliant plugin. Absolutely superb!!

  36. Roman Edirisinghe |

    FYI: This plugin doesn’t work with Wordpress 2.1. The problem I had was with using a static front page. For some reason, when this plugin was active, I could never get the home page to remain static. It always defaulted to the standard blog format.

  37. Olly |

    It doesn’t function at all in 2.1. I tried it earlier and changing the slug doesn’t result in a new custom field being created. As a result, visiting the old URL just gives an error.

  38. Mark (post author) |

    This plugin is not needed (and may give unpredictable results) in WordPress 2.1

    The functionality was added to WordPress core, so you should disable the plugin.

  39. YeOleImposter |

    I am running 2.1 I just changed the title to one of my posts and WordPress did change the slug. But the old slug now causes a 404.

    I thought that is what this plugin fixed, yet everyone is saying that this plugin is no longer needed?

Comment pages: [1] 2 »

Leave a Reply