Posted under: Portfolio

March 26th, 2011

How To: Permanently Redirect WordPress Pages using .htaccess

I’m sure we’ve all been there: when you or your client launched your WordPress website you thought the best thing to title one of your pages was “Foo” or some other such silliness. After a few months you realize that a better page name would be “Bar” and you want your URL to reflect that. So you login to your WordPress admin, change your “Foo” page title and slug to “Bar,” and click save. Done, right? Well, almost.

When you change a page slug, it obviously affects your URL.  Your old page used to be here:

http://www.yourdomain.com/foo

…and your new page is here:

http://www.yourdomain.com/bar

However, if you were to actually type your old address into your browser, you’re going to get a 404 Page Not Found Error. This matters because there are probably all sorts of references out there (in google’s search results, in your own website, on other websites, etc.) to http://www.yourdomain.com/foo, and you don’t want anyone who clicks on one of those links to get an error message.  You want them to go to your new URL: http://www.yourdomain.com/bar.

To do this, you’ll need access to the root folder of your WordPress install.  Make sure that whatever application you’re using can view hidden files, and look for the “.htaccess” file. Hopefully WordPress will have already automatically created one for you, but if not go ahead and create one.  Here’s an example of what the WordPress .htaccess file might look like:

# 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

Leave this bit of code there, and let’s go ahead do a Permanent 301 Redirect from your old page to your new page.  To do this, in our example, simply add the following line of code:

Redirect 301 /foo http://www.yourdomain.com/bar/

Now, if you save the file and then try to go to the “/foo” page, your browser should redirect your automatically to the “/bar” page.  When search engines crawl your site, they’ll update their records with this information as well.

Cheers!

By: | Comments Off