How to Add 301 Redirect in WordPress via .htaccess File: Beginner’s Guide
What happens when you click a link and, hoping to find something compelling, you’re hit with this page?
Like any other visitor who’s experienced the same thing, you’ll leave the website and never come back again.
A 404 error is a UX nightmare for many users—and 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 original URL, but it will also tell the search engines to index the new page instead of the old one.
But that’s just one use case of a 301 redirect.
In this post, we’ll guide you on how to add a 301 redirect in WordPress using the .htaccess file. We’ll also cover:
Let’s delve in.
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.
When a user clicks on an old URL, the server will send 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 we mean, suppose you had published a page with a long URL like this:
And after learning that a shorter URL is better for readability and SEO, you decide to shorten it.
The search engines and users will not know that you’ve changed your page URL. Anytime they try to access the previous page using the original, 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 new one.
A 301 redirect is not only used to fix broken links. You can also use it to
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 type of redirection 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 redirection 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 a .htaccess File?
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 a .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 using a tool like Updraft Plus 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”.
Click to open the public_html folder, and on your right side, you’ll see the .htaccess file.
If you can’t find your .htaccess file, go to the upper-right “Settings” menu and check the “Show hidden files” box to view hidden files in your root folder.
Step #2: Edit the .htaccess File
Click on the file and choose the Edit option.
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.
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.
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 add 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/
Now, 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.
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