Skip to content
New Rebranded LinoDash logoNew Rebranded LinoDash logo
  • Home
  • About
  • Contact
  • Newsletter
  • Blog
New Rebranded LinoDash logoNew Rebranded LinoDash logo
WordPress Tutorials

How to Add 301 Redirect in WordPress via .htaccess File: Beginner’s Guide

Written By:Editorial Team Updated onFebruary 13, 2023
301 redirect htaccess file

What happens when you click a link and, hoping to find something compelling, you’re hit with this page?

RsTmKsX oErvTZgiH7H0m02hFBsZIxyxrXGoKrqGkB4iQrxbMmiROXAy34E9XvH9XK3QeXLAvE45dZ5B

Like any other visitor who’s been down the same path, you’ll leave the website and never come back again.

Well, I understand that there are website owners out there trying to get creative with their 404 error pages.

hDiqdhT4bw6fytnGtjcxJpL8bAMJSMSKR0TktRh2xRsMt22Dv0SI2VjI1BmGTjZd3ME3fQtthjjYbpFY64W606mxFjO0xbBk cUVUBdUsRB62nS q6 9IOm73QeiCSWlx ggRUtvld0VqibT6Qak0ZGyuhTVBn DVpbSAMKLasuYkKLylh5vv87p7bYk6Q

But at the end of the day, the 404 error remains an UX nightmare.

One of the best ways to fix the issue is by using a 301 redirect to direct the broken link to a live page.

Not only will this provide a better user experience for visitors who use your old link, but it will also tell the search engines to index the new page instead of the old one.

Today, I’ll show you how to add a 301 redirect in WordPress via the .htaccess file. 

In a nutshell, you’ll learn:

  • What is 301 redirect
  • The difference between 301 and 302 and when to use each one of them
  • How to add a 301 redirect in WordPress using the .htaccess file as beginner
  • What to do if you can’t find your .htaccess file

Right off the bat, let’s get started.

What is a 301 Redirect in WordPress?

A 301 redirect is used to indicate that a web page has been permanently moved to a new URL.

UAakR5LIpMS9dPAKx4wKE55oPmbDNDoVvLvoUVFQ33zdzWMj1e7tH d00qoil JCBQtgFrZsnNetsoAgat SbCnXey7G6w v9tWlgi4tfi5kW6MpbTT57oY4pLlLhj

When a user clicks on an old URL, the server sends their browser the 301 HTTP status code, telling it that the page has been permanently moved to a new location. 

To give you the context of what I mean, suppose you had published a page with a long URL like this:

www.yourwebsite.com/7-things-that-you-can-do-to-create-a-better-morning-routine/

And after learning that shorter URLs are better for search engines, you decide to shorten it.

www.yourwebsite.com/create-a-better-morning-routine

The search engines and users will not know that you’ve changed your page URL. Anytime they try to access the previous page, they will get a 404 Page Not Found error. 

To fix the issue, you need to show them that you’ve moved your URL to a new location.

And that’s by redirecting the old page URL to the current one.

A 301 redirect is not only used to fix broken links. You can also use it to

  • Redirect an old domain to a new one
  • Redirect your website from  HTTP to the secure HTTPS protocol.
  • Fix non-www/www duplicate content issues
  • Redirect multiple pages or websites to a single URL after permanently merging them into one.

301 vs 302: What’s the Difference and Which is Good for SEO?

Both types of redirects mean that a previous page has been moved to a new one, but how long the redirection should last is what sets them apart.

As mentioned earlier, a 301 redirect indicates an old page has been permanently moved to a new one. However, the 302 redirect is used to temporarily redirect one web page to another one.

So basically, a 302 HTTP status response code tells a browser or search engine that the page has been redirected to a new page for a limited amount of time. 

Here’s when to use the 302 redirect:

  • A/B testing a web page for functionality or design
  • Getting client feedback on a new page without impacting site ranking.
  • Updating a web page while providing viewers with a consistent experience.
  • Broken webpage and you want to maintain a good user experience in the meantime. 
  • Want to A/B split-test the functionality or design of a webpage.
  • Want to get feedback about a new page without affecting the old page’s rankings.
  • Run a promotion and want to redirect visitors to a sales page temporarily.

The good thing with the 302 redirect is that you can restore the old URL after some time. So if you want a type of redirection that will allow  you to bring back the old URL after some time, then go for the 302 redirect.

What Is an .htaccess File?

JrsSDaN oEtqoxSUiuns6unygUTQYw eiNZsYtzYniPpkntofahlRtS2CvuqQUCgSJ9qek8vdyytrbS6T0p BM6H7YmWlgyMkKbu1IxHsDsQEpppv1MU7VtpbX2LleD6bmHS2kmkB7rW7Qpi6XW4di noCTkXPrdKIrVmDmAbsZ1ew4Hu0V4adWtnsPoJQ

The.htaccess (hypertext access) is a hidden configuration file that allows you to make changes to your website details without editing server configuration files. 

Using the .htaccess file, you can make changes like:

  • Redirecting URLs
  • Disallow specific IP addresses from accessing your website
  • Password protect specific directories on your server
  • And even change your website timezone

The .htaccess file  is only available for websites hosted on the Apache Web Server. Other web server software like NGINX doesn’t have an .htaccess file. Instead they use different rewrite directives which must be placed within the server block. 

How to Set up a 301 Redirect in WordPress Using the .htaccess File

The .htaccess file is a sensitive document for your WordPress site. A mistake in your code or incorrect setup of the file can break your website. 

In that case, I recommend that you backup your site before making any changes to the file. That way, you can easily restore your site in case something goes wrong.

Now, here’s how to add 301 redirects in WordPress via the .htaccess file.

Step #1: Locate Your WordPress Site .htaccess File

To make changes to your.htaccess file, you must first connect to your WordPress site server. There are two methods for connecting to your website’s server and finding your .htaccess file.

  • Via the FTP client like Filezilla
  • Or through the cPanel’s inbuilt file manager

In this case, I’ll show you how to find your .htaccess file in cPanel’s File Manager.

Login to your cPanel dashboard, go over to the Files section, and click on “File Manager”.

7ba FFsVz2k3atiFkKfwzUw8 ttrS5R9wV35TajvezYPVIQZ1eHgrRqocQ1hDu5I75nQkqU Qy4kqVvCRrBjaWTYDBhqxvmq9V5WOqjyvBFhrCKoCg T 7JLjdhIZlwG6PI

Click to open the public_html folder, and on your right side, you’ll see the .htaccess file.

qOKE4aTiNM8DsjmPy5PW5IrkF1YaFYpM95w2Q5dv1UcZ7hG5ybmVgLgmkdTx9Vd7spX14UDqGjPvlnpqmNgQb 9tOqtaNI2KBaixPr8PFgGMtGsvdYnUTzilybXpZKZjT MffbD W0 6bYbiJlU6CYm DswZRDDaShVp nulnTbIaDtSKkK8EpyfBo Cg

If you can’t find your.htaccess file, go to the upper-right “Settings” menu and check the “Show hidden files” box to enable viewing.

0XVlInLsnnNvSGeZs3kEgvZbjVoYo1i3CBg6iyTGW8zvZau0Wa 9EuwxrKj5WVjJqpIUM76A0Jol xiPN hOyROX0ce qMhpKqi yOfADL7lRO2 LtwGK a9Hk2qMKBDsJU 022FppS2LHY0cYGBjyAQO5J6dP4sCN 3FMSFBQfg

Step #2: Edit the .htaccess File

Click on the file and choose the Edit option.

quOZBfaBCPCC14IJCHL8cYBbzr76UwKlX8emsG5A3kjMHbPqU19tIurlRQfNzwS2s6EQoVNTAwXY18Ai c1IO3Mi0txb ONDezcjmTJEgxu5oshxZ9VAJDd0PQ6Jamj9Y Qw rtcUE oq0BWlotPFcXqCHnvP3SIzp7zwui8d7eTgBBeCB6Dpq75t08 w

You can also choose to download a copy of your .htaccess file to your computer before making changes, just in case something goes wrong.

When you open your .htaccess this is how it should look like for a basic WordPress  website.

MVwblUPGU2BxfA9tFHdQfJTczUTHrAPn7qlKq3tP2PhVegMIH5RaoA1PaKzMORtOkGuWeKVPLUr8 YMBkuAgFDsx X7COmLVsg2oR1mme zAWLcXngpA4L5t

However, some plugins, especially caching plugins, can also add some rules, so you don’t have to worry if you see more code than the example above—like this one here.

AfZN9oeZ0L9KIm1McL9R9iAv EX2hP0F2zU5Wu1BwSIxMxdBt9OXoZKhDefqdKsHbsI sX2qGVBkdvJ3iKHbw4FcUNMN1tY419IXZhPotwrn2T 2EA2uegxu6fDho bVfxn4LgnBTx7NtwJHfqlI42HhSFdO 14S0JlaZRgEscmRX HCQYBifZSHAz DHA

Now, to add 301 redirects via .htaccess file in WordPress, you need to add a piece of code after the “# END WordPress” line.

To redirect old pages or posts to a new one, you can use the following code.

301 redirect to oldpage.html http://www.example.com/newpage.html

Please remember to replace “oldpage.html” and “example.com” with the corresponding domain and url of your page. 

For example, I may want to redirect one of my pages, say:

www.linodash.com/best-wordpress-blog-today, to this page: www.linodash.com/best-wp-blog/.

Then here’s what the code should look like:

Redirect 301 /best-wordpress-blog-today https://linodash.com/best-wp-blog/

I hope you get it.

If you want to redirect an entire domain to a new one, you can use the following code snippet to make the changes:

<IfModule mod_rewrite.c>

  RewriteEngine On

  RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]

  RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$

  RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]

</IfModule>

Again, remember to replace “olddomain” and “newdomain.com” with the actual sites domain.

For example, let’s say I want to redirect my old domain, jamesoldsite.org, to my new domain, linodash.com.

Then, here’s the code that I’m supposed to add to my .htaccess file.

6ftYtcksqUKyIB2525 rrdAPHZ9

Once done, save the changes.

Wrapping Up

Adding a 301 redirect in WordPress can be a little bit challenging for a beginner and risky as well. One simple typo can break your website or even result in actions you never intended for your visitors.

As a result, I recommend backing up your website and even your .htaccess file before making any changes. 

If you don’t want to use the risk method to add 301 redirects, there are WordPress plugins that can help you with that. 

Here are plugins that you can use to 301 redirect your pages in WordPress without touching a line of code.

  • Rank Math
  • Redirection
  • All in One SEO
  • 301 Redirects
  • Quick Page/Post Redirect
Editorial Team

Content Writer and Marketer

James Njoya is a prolific BoFu writer, content strategist, and blogger with a passion for the B2B Saas industry. When he's not writing for his clients he's out there testing business software and publishing his findings on his blog.

Twitter Linkedin

Post navigation

Previous Previous
How to Create Tables in WordPress: Step by Step Guide
NextContinue
How to Start A Blog In Kenya And Make Money Online
  • Home
  • About
  • Blog
  • Contact
  • Privacy Policy
  • Affiliate Disclosure
Facebook Twitter Linkedin

© Copyrights Reserved 2023 | LinoDash Media

Scroll to top
  • Home
  • About
  • Contact
  • Newsletter
  • Blog
Search