Apache rewriting from one WordPress folder to another

Just now renamed a blog running under WordPress in Multisite mode (subfolder) and wanted to redirect still trying to use the old url to the new. Apache rewrite recipe follows.

This is a pretty basic mod_rewrite configuration as they go, but since it took me a few minutes to get it right I thought it might save someone else a bit of time.

Once upon a time there was a blog called http://blogs.example.com/oldblog. After awhile its owners decided they’d rather call it http://blogs.example.com/newblog.

Changing the blog name wasn’t a big deal. All we had to do was dump the database using mysqldump[1], do a

perl -pi -e 's/oldblog/newblog/g' blogs.sql

and then reload the db[2] (or copy the searchreplacedb2.php script into the site root and use it to do the search and replace).

Doing rewrites of WordPress urls can be a tricky business because so much of the “magic” involved in making WordPress work, particularly in multisite mode, is in some fairly involved rewrite rules embedded in the main .htaccess file in the root folder. As a result there’s really no margin for error in syntax.

For my solution the following lines were inserted in the Apache virtual host block for the blog, preferably after the </Directory> statement:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} oldblog
    RewriteRule oldblog/(.*) /newblog/$1 [R=301,L]

The rewrite worked without the RewriteCond statement, but I think it’s safer to include it.

[1] The syntax for mysqldump would be something like:

mysqldump -u root -p --datatabase blogs >blogs.sql

[2] Reloading the db would be done using a statement like:

mysql -u root -p blogs <blogs.sql

Note: I really wanted to use superscript for these notes, but the font applied by this wordpress.com supplied theme was just too damn small. So I fell back to using bracketed numbers like we did back in the bad old days when TeX was the weapon of choice for computer-saavy grad students. I printed all the papers I wrote for my Masters degree on a daisy wheel printer (whose CP/M driver was the one and only piece of software I ever wrote in assembler — in between trial calendar calls!) formatted with TeX for CP/M — after composing them using Eric Meyer’s VDE.

This entry was posted in System Administration on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).