How To Set Up A .htaccess 301 Redirect In WordPress

For a number of reasons, you may find that you need to send traffic from one page on your blog to another. An effective way of doing this is to use a htaccess 301 redirect.

When I released the custom menu plugin, I made the mistake of notifying a website that it’s URL was http://www.clubwordpress.com/club-wordpress-custom-menu/, so when I checked my analytics report, I noticed that a load of traffic was being sent there and getting my 404 page.

Getting the link changed could have been tricky, so instead I needed to set up a redirect in my .htaccess file to send traffic from:

http://www.clubwordpress.com/club-wordpress-custom-menu/

to:

http://www.clubwordpress.com/wordpress-custom-menu-plugin/

Here is how to do it:

1. Make a backup of my original .htaccess file

This is very important. It is extremely easy to make a mistake or typo in the .htaccess file which can take down your whole website. If this happens, all you need to do is swap your original .htaccess file into your blog and sort out where you went wrong.

2. Open the .htaccess file for editing

You will now need to get your .htaccess file from your webserver using FTP and open it for editing.

Wordpress .htaccess

If you can’t see your .htaccess file, then your FTP client may not be configured to show hidden files. To find out how to set this type “[FTP client name] show hidden files” into Google and you should find out how to do it. e.g. Filezilla show hidden file names.

If you are using permalinks on your blog, then your .htaccess file may look something like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

This is all stuff for re-writing your URLs and shouldn’t be messed with. Any edits that you make should be outside of the #BEGIN and #END Wordpress lines.

3. Add the redirect line to the .htaccess file

Now add the redirect. You need to specify the relative URL you are redirecting from and the full URL you are redirecting to. Getting these wrong can take your blog down. I know because I have allready tried it.

So, the redirect should look something like this:

redirect 301 /old-url/ http://www.myblog.com/new-url/

We are using the 301 to notify search engines that the page has moved permanently. If it was a temporary redirect we would use 302 instead.

4. Upload the new .htaccess file

Once you have added the redirect, you can now upload your file to your blog. If your .htaccess if readonly, you will nead to make it readable before uploading.

You can now test the redirect. If your site goes down, then you have probably made a typing error, so put the original backup of the .htaccess back while you find what you did wrong. You did make a backup didn’t you?

Once you have found the error, upload again and hopefully you should be good to go.

Posted in Administration, Hacks | No Comments