The default Wordpress URLs aren’t very search engine unfriendly – http://code.mincus.com/?p=3
Under the Wordpress options, it is easy to change this to something much nicer looking, for example: http://code.mincus.com/3/adsense-notifier – But that could lead to losing the PR that you’ve built up from the old links if you make the change after your blog is established or duplicate content issues.
For people using Apache, it should be relatively easy to add 301 permanent redirects using rewrite maps to fix both of these issues.
Three changes are needed to implement this:
Put this file somewhere where apache has access to execute it and name it something like mod_rewrite_map.pl. Update the three variables with your own settings for the DB name, username, and password. Make sure you give it execute permissions. This program will run in the background and give the pretty URL name when passed an article ID. Test it by running it then typing in an article ID and hitting enter. If a friendly URL name comes back, you’ve got it working right.
#!/usr/bin/perl
use DBI;
$|=1;
my $db_user = "database username";
my $db_pass = "database password";
my $db_name = "database name";
{
my $DBH;
my $STH;
sub startdb {
if ( !$DBH ) {
$DBH = DBI->connect( 'DBI:mysql:' . $db_name, $db_user, $db_pass );
my $query = 'select post_name from wp_posts where ID=?;';
$STH = $DBH->prepare( $query );
}
}
sub stopdb {
if ( $DBH ) {
$DBH->disconnect;
}
undef( $DBH );
}
sub execute_db {
my $post_num = shift;
my $post_name = '';
startdb();
$STH->execute( $post_num );
$STH->bind_columns( \$post_name );
$STH->fetch;
return( $post_name );
}
}
while(<stdin>) {
chomp;
print execute_db( $_ ) . "\n";
}
stopdb();
Next, add the following lines to your httpd.conf. They tell apache to load the rewrite map program into memory and when to use it. While you can place these in a virtualhost, they are not virtualhost specific. I just put them there in my httpd.conf because it makes it easier for me to remember my logic in setting it up.
RewriteEngine On RewriteMap name_recmap prg:/path/to/mod_rewrite_map.pl
Last, add the following lines to your Wordpress .htaccess – They tell apache to use your rewrite map and when. In this setup it does the following:
^p=([0-9]+)$ – If the query string is requesting a page, and is only digits
rewrite the URL to be ID/Pretty-URL-Name/
RewriteCond %{QUERY_STRING} ^p=([0-9]+)$
RewriteRule ^.*$ %1/${name_recmap:%1}/? [R=301,NC,L]
That’s how I have it setup here on code.mincus right now. But I recently found this Redirect Wordpress plug-in that seems like it will do all of this and more only easier. If anyone gives it a try let me know how it works out.
UPDATE(10/19/2006): I have been using Scott Yang’s Permalink Redirect Wordpress plug-in for a few months now and love it. It’s a must have plug-in if you are using Wordpress and have even a passing interest in SEO and rankings.
August 7th, 2006 at 4:22 am
I’ve been using the above plugin for a long, long time and it works great.