How to Add 301 Redirects to WordPress via .htaccess Files

Changing a page URL or its domain name seems easy, but redirecting users from the old page to the new one is essential if you don’t want to lose your website traffic from direct or organic clicks through to your old domain.

To make it easy for search engine crawlers, we can use a 301 redirect from the old website to the new one. 301 redirects are commonly used by most WordPress users when they want to change their domain names, but they can also be used from one page to another too.

What is a 301 redirect in WordPress?

A 301 redirect is an HTTP response status code that refers to a permanent redirect from one URL to another. As stated, it’s used to indicates the permanent moving of a website to another website, or URL to another URL.

How you implement the redirect will determine whether it’s site-wide or individual URL basis. For example, this will redirect one URL to another:

Redirect 301 /old-post https://websiteb.com/new-post/

(This will implement the redirect as follows: https://websitea.com/old-post to https://websiteb.com/new-post/

When the old URL addresses are requested, users are automatically redirected to the new ones with the help of 301 redirects. We can also do a site-wide redirect, which will redirect all of the pages on your website to the same URL on the new website (I’ll get to this in a minute).

301 redirects are an important part of website management and search engine optimization, as they tell the search engine crawlers that your page area will be permanently moved to a new page. This way, they are more likely to recognize your brand new URL and index it instead of the old one. As a result, you can maintain and position all your SEO values.

What is a .htaccess file?

Hypertext Access, also known as .htaccess file, is a configuration file located in the root of the central directory, usually under the administration of Apache. It can be seen as part of the web server and allows redirects or other functions to be flexibly activated and deactivated without directly editing the server configuration.

An .htaccess file also allows you to control access to a specific directory on your web server. It can be supplemented or customized with several configuration settings to ensure the security of a folder or website at a high level.

For this reason, this type of file is usually hidden and does not have a folder name to prevent unauthorized users from accessing and customizing it. Otherwise, this can have negative effects on the website, such as inaccessibility.

Why should I implement 301 redirects using the .htaccess file?

When users request a page URL, your servers look for the .htaccess files as they are responsible for displaying your website’s permalinks. These files contain special instructions for requests, redirects and security when you change your permalink structure.

The cooperation of 301 redirects and .htaccess files should help you to redirect your visitors from the old website structure to the new structure or your new domain name. This is beneficial to improve user experience so that they don’t get 404 errors when accessing your remote page.

How to implement a 301 redirect with .htaccess files in WordPress

There are only a few steps required to set up a 301 redirect in WordPress using .htaccess files.

Search for and download your .htaccess file on the WordPress website

To start, you’ll need to connect your web server via FTP to edit your .htaccess file. If you have difficulty accessing it, you should ask your hosting provider for help.

For most people though, you can usually find the .htaccess file using cPanel’s built-in file manager tool:

Screenshot

As already mentioned, the .htaccess file is stored in the root directory of your WordPress website. You can therefore find and download it using the file manager tool in your cPanel dashboard, or you can edit it directly from within file manager (though be careful, as you could break your website this way).

Adding 301 Redirects to Your .htaccess File: A Comprehensive Guide

Now comes the most important step: adding the proper redirect code to your .htaccess file. While the code might look technical at first glance, we’ll break everything down into simple, actionable steps with detailed explanations.

Before You Begin: Creating a Backup

Before making any changes to your .htaccess file, always create a backup. This ensures you can quickly restore your site if anything goes wrong.

  1. Download a copy of your current .htaccess file
  2. Store it in a safe location on your computer
  3. Proceed only after confirming you have a working backup

1. Redirecting a Single Page to Another Page

This is useful when you’ve moved or renamed a specific post or page.

Redirect 301 /old-URL https://yourdomain.com/new-URL

Example:

Redirect 301 /summer-sale https://yourdomain.com/seasonal-promotions

This will redirect visitors from https://yourdomain.com/summer-sale to https://yourdomain.com/seasonal-promotions.

Note: The path in the first part of the redirect (/old-URL) should only include the part after your domain name. Do not include https:// or your domain name in this first part.

2. Redirecting a Single Page to Another Domain

If you need to send visitors from a page on your current site to a page on a completely different domain:

Redirect 301 /original-page https://differentdomain.com/destination-page

Example:

Redirect 301 /partner-offer https://partnerwebsite.com/special-deal

3. Redirecting Your Entire Website to a New Domain

When migrating your entire WordPress site to a new domain, use this code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]

Code explanation:

  • RewriteEngine On – Activates the rewrite functionality
  • RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR] – Checks if the requested host is your old domain (non-www version)
  • RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC] – Checks if the requested host is your old domain (www version)
  • RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC] – Redirects all pages to the same path on your new domain
  • $1 – Preserves the original path (everything after the domain name)
  • [L,R=301,NC] – Flags that make this a last rule (L), permanent redirect (R=301), and case-insensitive (NC)

4. Redirecting a Specific Post to Another Post

For redirecting individual posts or pages with more specific examples:

Redirect 301 /original-post-slug https://yourdomain.com/new-post-slug

Real Example:

Redirect 301 /301-post https://passwordprotectwp.com/how-to-redirect

This redirects visitors from www.passwordprotectwp.com/301-post to www.passwordprotectwp.com/how-to-redirect.

5. Redirecting a Specific Category or Tag Page

If you’ve renamed or consolidated categories or tags:

Redirect 301 /category/old-category https://yourdomain.com/category/new-category
Redirect 301 /tag/old-tag https://yourdomain.com/tag/new-tag

6. Redirecting HTTP to HTTPS

If you’ve recently installed an SSL certificate:

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

7. Redirecting www to non-www (or vice versa)

For consistent domain structure:

www to non-www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

non-www to www:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Where to Place the Code

The placement of redirect code within your .htaccess file matters:

  1. For domain-wide redirects (like domain changes or www/non-www redirects), place the code at the beginning of your .htaccess file, before any WordPress rules
  2. For individual page redirects, place the code after the WordPress rules (typically after # END WordPress line)

Testing Your Redirects

After implementing redirects, it’s critical to test them:

  • Verify that all parameters and tracking codes are being passed correctly
  • Clear your browser cache or use incognito/private browsing mode
  • Visit the old URLs to confirm they properly redirect
  • Check for redirect loops or chain redirects (multiple redirects before reaching the final destination)

Conclusion

Getting the right 301 redirects in place is important if you want to move an entire website to a new domain name, or simply move one page to another. Beyond preserving SEO value, 301 redirects make the user experience better by automatically guiding visitors to your new content instead of leading them to the old pages, which are replaced by 404 errors.