How to setup 301 redirects in WordPress using .htaccess

Redirection is the process of sending users to a new page, rather than the page they visit. The process consists of adding specific entries (called 301 redirects) to one particular file known as Htaccess.

Webmasters use to set up redirects for old URLs. Its positive impacts include improving SEO, user management, and community experience.

It is easy to create and manage redirects on a WordPress website. By installing a WordPress plugin or creating the HTACCESS file, you can efficiently redirect URLs on your site.

In this article today, we learn how to set up 301 redirects in WordPress using a .htaccess file; a file at the root of your website and includes URL redirection and access control features.

Besides using htaccess for redirection, as mentioned above, we can use a plugin for the purpose. However, for speed and manual configuration, we can play well with an htaccess file.

Know before you proceed

Important Facts

(1) Backup your htaccess file FIRST. (Log into your Hosting cPanel -> File Manager and download a copy of your htaccess file to your computer)

(2) If you experience site issues after updating the htaccess file, you should immediately log into your Hosting cPanel -> File Manager, and proceed to upload the backup copy of your htaccess file.

What are 301 redirects?

301 redirects refer to instructions that inform web browsers or search engine bots about the new (permanent) location of an Internet resource. For example, after you rename a page’s URL on your site, you can proceed to create a 301 redirect that sends visitors to the new version of the old page.

What is a .htaccess file?

It’s an Apache Web Server based configuration file. It contains instructions to rewrite URLs, redirect, or manage the access controls of specific URLs on a website – used to manipulate Apache’s usual routine of how it serves the browser’s requests for web pages.

An htaccess file resides at the root of a website. It is hidden by default; however you can show it by changing the directory settings.

Creating an htaccess file is simple and easy. In a WordPress website, you can create an htaccess file by following the listed methods below.

  • In Yoast SEO, head over to SEO -> Tools page, and proceed to click on the File Editor. Now, you can create an htaccess file or make changes in the current one.
  • Create a simple file in a text editor, save it, and upload it to your website root. After uploading the file, remove the TXT from its extension, and place a DOT at the start of the file name.

Now you’ve created an htaccess file; you can proceed to make changes and add 301 redirects for the old URLs.

What are the benefits of using 301 redirects?

A 301 redirect plays well for several reasons and scenarios, including:

  • Useful for sending users to new versions of pages or posts on your website
  • An excellent choice for redirecting HTTP traffic to HTTPS
  • Helps you redirect users to custom 404 pages
  • Manages access control – manages users who try to browse sensitive information
  • Enables you to broadcast SEO signals – you can redirect relevant traffic to a masterpiece on your site

Although it requires manual editing of the htaccess file, you can add more entries without letting your site speed go down.

How to set up 301 redirects in WordPress using the htaccess file?

We are interested in two types of redirects:

  1. 301 Redirect: Use for permanent redirection of an old page to the new destination page.
  2. 302 Redirect: Use in case you temporary want to redirect a page to a new resource.

Syntax structure

  1. 301 Redirects: Redirect 301 “old-page” “new-page”
  2. 302 Redirects: Redirect 302 “old-page” “new-page”

Take care of the spaces, double quotes, and syntax structure.

It’s a two-step process, beginner friendly, and helps you quickly set up 301 redirects on your WordPress website, without using a plugin.

First, check whether you have an htaccess file on your website. If yes, you can skip over to STEP 2.

STEP 1: Create an htaccess file

If you can access your Hosting cPanel -> File Manager, you can create a new file at the root of your WordPress site, and proceed to rename it as .htaccess.

Alternatively, you can create a simple text file on your PC, and upload it via File Manager or FTP. Moreover, the Yoast SEO plugin also provides an option to create and edit your site’s htaccess file.

After creating and uploading the file, make sure you rename the file to .htaccess.

STEP 2: Setup 301 redirects

The next step after creating an htaccess file is to set up 301 redirects. It is simple, and you can easily do so by editing and saving the htaccess file.

You can edit the htaccess file in your Hosting cPanel -> File Manager or via an FTP client such as File Zilla. You can also use the Yoast SEO plugin for making changes to the htaccess file.

To get started, here is an example to set up a simple 301 redirect.

Redirect 301 “old-page” “new-page”

Note the spaces between the words in syntax, where old-page represents the existing old version while new-page represents the new destination version of the page.

Here is a list of useful commands for htaccess 301 redirects.

(1) Redirect visitors from old page to a new destination post

Redirect 301 “old-post” “new-post”

(2) Redirect all of your site traffic from HTTP to HTTPs

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Before redirecting your site traffic from HTTP to HTTPs, you should have configured SSL on your WordPress website.

(3) Redirect 404 page to Home Page

Open the 404.php file inside your theme’s folder and add the following code in there – save changes at the end.

header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: “.get_bloginfo(‘url’));

(4) Send relevant traffic to a masterpiece on your site

Redirect 301 “seo-tools-for-beginners” “seo-tools-mega-list”

Wrapping up

WordPress supports plugins to extend your site’s functionality. However, if you prefer page speed, you can set up 301 redirects via htaccess and prevent your site from experiencing speed downgrades.

Most of the times, you can see an htaccess file already present at your site root. You can easily create one; however, by creating and uploading one or by using the Yoast SEO plugin’s (File editor) feature.

It is easy to make changes to an htaccess file. You can start by adding 301 redirect entries to the htaccess file.

Most of the times, you’d like to use the htaccess file for redirecting old URLs. Also, the htaccess file plays well for redirecting your site from HTTP to HTTPs.

With a little knowledge, you can master using 301 redirects as a beginner. Read more about using the WordPress htaccess file as a beginner.

Also, don’t forget to share your thoughts. Let us know your experience by joining the conversation in the comments.

More on wpcrib

1 comment

  1. Hi,

    You can try out the following syntax.

    Redirect 301 “old-post” “new-post”

    You just need to replace the old-post and new-post text in above syntax.

    Let me know if you still face problems. 🙂

Add comment

Join discussion and make an impact. Your email address will not be published.

GDPR is going into effect on May 25, 2018. Learn more in our new GDPR section. You can also view changes to our Privacy Policy.
We use cookies to provide a personalised experience for our users.