--- title: "Permanently redirect WordPress pages" kind: article slug: permanently-redirect-wordpress-pages created_at: 2008-04-15 tags: - General - Wordpress - apache - apache2 - rewrite - mod_rewrite - permalinks --- After my trip to Mephisto some time back, I noticed that some pages were accessible from different URLs. After moving back to WordPress, these permalinks no longer work. I'm running WordPress with Apache2, so it shouldn't be too hard redirect those old permalinks to their new locations. (That's what rewriting is all about anyway). Here is the default .htaccess file generated by WordPress:
# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


# END WordPress
As I said, it's generated by WordPress. It would be very unwise to edit this, because our changes might get lost in the future. The solution is simple. Add another block above the generated one that does permanent redirects. In this example I rewrite pages/svnsheet to http://ariejan.net/svncheatsheet.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pages/svnsheet$ http://ariejan.net/svncheatsheet [R=301,L]


# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


# END WordPress
FYI, the R=301 means the browser (or search bot) receives a "Moved Permanently" message. L means this is the last rewrite rule to read. Of course, you can go nuts and add all sorts of funky regular expressions to rewrite URLs. The nice thing is that WordPress will only replaces the block between the BEGIN and END tags, keeping your rewrites in tact. Note: don't forget to restart apache if your rewrites don't seem to work.