WordPress is a superb CMS tool or the majority of web projects, big or small.
It is most often simplicity itself to operate and manage, but there are certain things which it does not provide simple functionality for.
A prime example of this is when you need to change your “Permalink” setup within it.
Whilst it offers an easy interface for changing the default permalink scheme of your site it does not provide any functionality around managing the “fallout” of this change. Namely that existing links on other sites, emails, presentations, or, most importantly, search engines like Google, Bing or Yahoo will suddenly fail / return an error when potential visitors use them.
Now there are a number of solutions for mitigating this effect, including a number of WordPress plugins, but in my experience these fail to deliver a simple and sophisticated solution which employs an efficient use of resources.
By far the best solution in this scenario is a simple, if a little technical, fix.
Without getting into borish detail we’re going to be looking at a level “under” WordPress itself. At the “service” which provides the platform on which WordPress sits. The most common implementation of WordPress is on “Apache” so we’re starting there.
A key object to understand when working with Apache is its “.htaccess” file, which is in the “root” of every website it serves. This file allows configuration of some powerful functionality within Apache and as such you must be VERY careful when working with it. Without it, or with an incorrectly configured version, you will likely render your site inaccessible (until you fix the error). So backup this file before you touch it, so you can return to this earlier version if needed.
Right. To solve the changed permalinks problem we simply need to understand that by changing permalinks within WordPress you have essentiually re-written the URL for every single post & page within your site. The problem lies in the fact that the Internet does not feature any mechanism to efficiently tell everyone else you’ve done so proactively, meaning the rest of the world will still be looking for your pages and posts under the previous permalink structure.
At this point lets jump to an example to better illustrate the scenario.
Let’s say I had my site here setup under the default WordPress permalink structure; “http://www.richardburley.com/2012/07/24/thispost” but I wanted to change it to the simpler, shorter and clearer “http://www.richardburley.com/thispost“.
Well, the first step in this most efficient of solutions is simply to change the permalink structure within WordPress (Settings -> Permalinks) but be ready to implement the next step quickly as the moment you change it all existing external links pointing to your site will start failing, it’s probably best to prepare to do so by working with Apache’s .htaccess file as follows;
So, let’s now edit .htaccess via either our server’s file browser directly or via something like FTP.
In there you will find a section which looks something like the following for your average WordPress install;
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
All we need to do in this case is simply add the following line (all on one line);
“RedirectMatch permanent ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9-/]+) http://www.richardburley.com/$1”
Between “RewriteBase /” and “RewriteRule ^index\.php$ – [L]” lines so that our WordPress section now looks something like;
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RedirectMatch permanent ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9-/]+) http://www.richardburley.com/$1
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
It’s important to note that your section MUST include the “RewriteEngine On” line, but that WordPress should have put that in already by default. It’s also important to note that you may have multiple “<IfModule mod_rewrite.c>” sections within your .htaccess file, and it is definitely recommended to work within this WordPress “area” of .htaccess for clarity going forward.
Once you have completed this step (obviously replace my website / domain name with yours!) all exisiting / “old” URLs will now work again as we have setup Apache itself to directly “rewrite” the URLs it receives from visitors before WordPress even sees them, which is by far the most resource efficient method of catering for the huge numbers of old URLs you may have floating around in email / on search engines out there the world over.
Oh and never fear fellow Microsoft users, I’ll be following this up at some stage with a similar guide for anyone using IIS to host WordPress in place of Apache. In the meantime let me know if I can help!
Further info and more examples can be found on the sites below;
- http://httpd.apache.org/docs/current/mod/mod_rewrite.html
- http://www.sitepoint.com/apache-mod_rewrite-examples/
- http://www.webmasterworld.com/forum92/4332.htm