How To Setup 301 Redirects In WordPress Using HTACCESS

Last Updated on 15th October 2019
Table of Contents

When you delete a page or change its URL on your WordPress website, visitors to your website can no longer access the page via the old URL.

If they try, they’ll land on a blank page showing them some kind of ‘not found’ template, which is confusing and not good for your customer experience. This is called a 404 page.

This isn’t good for a few  reasons. Firstly, any traffic and links going to that page will disappear. Secondly, Google doesn’t like it.

Visitors to your website don’t want to waste time on broken pages that don’t exist!

So when you change a URL, or delete a page, the correct thing to do is to create a 301 redirect sending anyone who lands on the old page, to a new one.

What is a 301 redirect?

Here’s a simple explanation of what a 301 redirect is:

“A 301 redirect tells internet browsers that the page in question has moved to a new location, then sends visitors to that new location”.

Simple right? Old page gone, new page here.

There are various different types of redirects, but a 301 redirect is a permanent redirect. This is the appropriate redirect to use when the old page has been deleted, or the URL changed permanently.

This is also an essential change to make if you want to protect your SEO rankings for a particular page, because a 301 redirect tells Google that:

“Hey this page has moved, but it’s exactly the same page, so rank it basically the same o.k?”.

Rankings (generally) do not fluctuate when changing the URL via 301 redirect.

When should you use a 301 redirect?

Here are some common scenarios where you should use a 301 redirect on your WordPress or WooCommerce website:

  • You change the URL of a page or post. The old page or post should be 301 redirected to the new page or post URL. This will ensure that any links pointing at the old URL will pass on that benefit to the new URL, and it also ensures any marketing campaigns  referencing the old URL still work.
  • You delete a page or post. This is a similar case to the above. If you just delete a page or post without replacing, you should 301 redirect the page or post the most relevant alternative, not just send every broken link to your homepage.
  • You want to combine multiple posts into one. Let’s say you have lots of small posts around a particular topic and want to combine these posts into one mega article. You could 301 redirect the small posts to your new mega article!
  • You change your domain name. This is very common with WordPress websites and site migrations. If you move your WordPress website to a new domain you will need to 301 redirect the old domain to a new one. If you have a large site, or have undertaken any kind of sustained SEO work, I would advise that you consult your SEO specialist before carrying out this WordPress 301 redirect.

What is .htaccess?

The .htaccess file is a configuration file for your WordPress server that handles things like site access, redirects, security and URL shortening.

This file is very important and even tiny errors can break your website, or affect the ability of your visitors to access your site, so take care when editing it and always back up first.

How to setup a 301 redirect on WordPress using the .htaccess file

Here is the process for setting up a 301 redirect using .htaccess

  1. Back up. Seriously. First things first, perform a full backup of your website. It’s incredibly important that you backup not only your website, but that you download a copy of your .htaccess file before trying to edit it.
  2. Locate your .htaccess file. You can do use this FTP or cPANEL file manager, depending on your hosting provider. Good hosting providers use cPanel, as it is the industry standard hosting platform.
  3. Add your redirects then save or upload the new file. If you have cPanel, you can edit the live file (provided you download a copy first) and avoid having to use FTP. If not, use FTP to download a copy, make your edits, then upload the new file back to the server.

Step 1 – Backup your website

If you use a good hosting provider like Siteground who use cPanel, good for you – you can perform backups direct from cPanel.

wordpress htaccess redirect

Siteground have a backup tool that allows you to back up specific files or databases and then restore them if necessary in a few clicks. This is particularly useful if you have multiple websites on one hosting plan and only want to restore one of the websites.

If you don’t have a hosting provider with cPanel and an easy to use backup tool built in, I would recommend ManageWP who plug into your WordPress website and allow you to backup and restore on demand.

wordpress htaccess redirect

2. Find your .htaccess file

There are two ways of finding your .htaccess file. Connecting to your website via FTP, which can be difficult if you are non technical. The second method is to locate your File Manager in cPanel and navigate to /public_html/

This takes you to the root of your website files.

Just a note. If you use the File Manager approach, some web hosts make the .htaccess file hidden by default so you will have to find a box to show hidden files or ask them to change this.

On hosts like GoDaddy this can be particularly difficult, so your best bet is using FTP if you are with GoDaddy and can’t find the file.

Here is what the .htaccess file looks like.

wordpress htaccess redirect

Now before progressing to the next step make sure you backup and store a safe, unedited version of your htaccess file.

3. Add 301 redirects to your WordPress .htaccess file

Now it’s time to actually open up your .htaccess file and put your WordPress 301 redirects in place. Start by downloading the file.

To open the .htaccess file you will need to right click, choose ‘open with’ and click on Notepad.

To 301 redirect a single page or post, add the following code snippet for each page or post you wish to redirect:

Redirect 301 /old-url

With the above script, you don’t need to include the full URL for the existing domain.

So if I wanted to redirect my About page to my Home page, for example, the code would look like this:

Redirect 301 /about-us

To redirect your entire website to a new domain

RewriteCond %{HTTP_HOST} ^ [NC,OR] RewriteCond %{HTTP_HOST} ^ [NC] RewriteRule ^(.*)$$1 [L,R=301,NC]

The above code will keep your old URL structure in tact, so for example /shop will remain /shop when moving from the old domain to the new domain.

If in doubt, make sure you consult your SEO specialist before making any WordPress 301 redirects on a large scale.