Gary Court

  • Home
  • About
  • Blog
  • Contact

Blog » Post

« PHP OSGi Framework Concept Installing OpenWRT on the Linksys WRT54GL »

Changing Permalink Structure in WordPress

Permalinks are a necessity in today’s online world. With the speed at which information is moving, it needs a way of always being referenced to without it “disappearing”. Permalinks are nothing more then a URL that will always point to the target information, regardless if it moves or not. WordPress has a built in permalink feature for posts and pages which works as intended. Unfortunately, permalinks are permanent, and if you don’t plan ahead, you could be stuck with a permalink structure that you do not like. I initially thought that the permalink structure I had setup on my website was well thought of and practical. However, in practice, I changed my mind and wanted a different permalink structure. I also didn’t want to break any links people had already made to my site. Here’s how I went about it…

Changing Permalink Structure

My initial permalink structure for posts was structured as such:
http://garycourt.com/blog/post/13

The problem with this structure is that just by looking at the URL, you are unable to determine what the content is about. A more common approach to permalinks (by other blogs) is to have the title in the URL, which I decided on, like so:
http://garycourt.com/blog/post/changing-permalink-structure-in-wordpress/

Now, luckily for me, it is fairly simple for me to make this change while maintain backwards compatibility with the old permalink structure. How? Well, the only difference is that the old URL always ended in a number, while the new URL always ends in characters. We can write rules that will be able to easily determine the difference.

WordPress keeps it’s URL structure rules in .htaccess, in the root of the WordPress directory. The rewrite rules are written using Apache’s mod_rewrite.

First, to maintain backwards compatibility with the old permalink structure, I copied the old URL rewrite code that finds blog posts. I then pasted this code, with some modification, above WordPress’s URL rewrite code.

# Begin Old Permalinks
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=6]
RewriteRule ^blog/post/([0-9]{4})/?.*$ - [S=5]  #Prevent breaking of year view
RewriteRule ^blog/post/([0-9]+)/trackback/?$ /index.php?p=$1&tb=1 [QSA,L]
RewriteRule ^blog/post/([0-9]+)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?p=$1&feed=$2 [QSA,L]
RewriteRule ^blog/post/([0-9]+)/(feed|rdf|rss|rss2|atom)/?$ /index.php?p=$1&feed=$2 [QSA,L]
RewriteRule ^blog/post/([0-9]+)/page/?([0-9]{1,})/?$ /index.php?p=$1&paged=$2 [QSA,L]
RewriteRule ^blog/post/([0-9]+)(/[0-9]+)?/?$ /index.php?p=$1&page=$2 [QSA,L]
</ifmodule>
# End Old Permalinks

If your wondering what I changed, I added the first 6 lines (which I copied from the beginning of the WordPress’s rules, and added the seventh line to prevent breaking of year view (since this rule must always come before post view rules).

I then went into WordPress and changed the permalink structure from:

/blog/post/%postid%

to:

/blog/post/%postname%/

Now, by going to either permalink structure, they will get the same post.

Forcing New Permalink Structure

Ideally, we want all users using the old permalink structure to update their links to the new structure. The HTTP protocol has a response code for that purpose: 301 Moved Permanently. This redirect response will send back a new URL which the content being requested can now permanently be found at.

I found a really nice WordPress plugin that does this extremely easily: Permalink Redirect. Essentially this plugin will check the requested URL against a post’s permalink, and if they don’t match, sends a 301 Moved Permanently response for the new permalink URL. All you have to do is install the plugin, and your done!

Conclusion

There are many ways of changing your permalink structure without breaking pre-existing links, and this is just one way. Note, however, that all target permalink structures may not be possible. I got lucky and was able to change my structure effortlessly.

Leave a Reply

Information

  • Author

    Gary Court
  • Posted

    Wednesday, February 1st, 2006 at 8:35 pm
  • Category

    • Personal
    • Web Development
    • Tutorial
    • WordPress
  • Tags

    • permalink
    • structure
    • wordpress
    • mod_rewrite
    • 301
    • blog
    • compatibility
    • post
  • Response

    • Comment
    • Trackback
  • Syndication

    • RSS 2.0 Comments
  • Related Posts

    • Latest Gallery pictures in WordPress
    • Gallery Available
    • First Post
    • Developing a Wordpress Theme
    • PHP OSGi Framework Concept
CourtNET

© 2005 Gary Court. All rights reserved. | Valid: XHTML CSS | XFN | Powered by WordPress & Gallery 2.