Home » Web Tutorials » WordPress » 5 Ways to Add 301 Redirects in WordPress for Deleted and Updated URLs

5 Ways to Add 301 Redirects in WordPress for Deleted and Updated URLs

Content management is an essential part of running any website. You need to frequently delete old content and update the URLs of the pages. Although you can do these stuffs within WordPress administrator panel, the problem comes when users and search engines try to visit the old non-existent URLs on your page. If the page is not found on your site, they will land on a 404 page leading to bad user experience and SEO problems. To avoid this, you have a mechanism called redirection to automatically send users and search engines to a new URL. This is called a 301 redirect, which happens on the server without showing any error message to the visitors. Search engines can also follow the redirect and update their records to use the new URL.

Why Do You Need a 301 Redirect?

You may need a 301 redirect when you:

  • Delete an old page and create a new page with the same or updated content at a different URL.
  • Merge two pages into a single page.
  • Move your website to a new domain name.
  • Move content to a different location, such as a subdomain or subdirectory.
  • Change the URL of an existing page.

In all these cases, visitors may still try to access the old URL through links, bookmarks, or search results. A 301 redirect automatically takes them to the new URL instead of showing page not found error. For SEO, a 301 redirect also helps search engines understand that the old URL has permanently moved to the new one and that the new URL should be treated as the replacement page.

Add 301 Redirects in WordPress

There are multiple ways to setup 301 redirect in WordPress.

  • Using free plugins like Redirection to manage simple and regex redirects or use premium plugins like Rank Math or Yoast SEO. This the easiest option and can be done directly from your WordPress administrator panel.
  • Manually add redirects using .htaccess file. For this, you need to access the WordPress installation directory on your server remotely through FTP or using the File Manager app in your hosting panel. Alternatively, you can use your theme’s functions.php file within the WordPress administrator panel.
  • Using the redirect manager feature from your hosting account and add re-directives in server configuration file.

Let’s explore each option in detail.

1. Using Redirection Plugin

This is a dedicated plugin to manage redirects within your WordPress setup without adding any entries in .htaccess file.

  • Install and activate the Redirection plugin on your site.
  • Go to “Tools > Redirection” section.
  • Click the “Add redirection” button on top to reveal the setup options.
  • Enter your old URL in “Source URL” box and the new URL in “Target URL” box.
  • Leave other options as they are and click the “Add redirect” button below to save your changes.
Add Redirect in WordPress with Plugin

You can setup a single page redirect or use regex to setup logical redirection in bulk. The plugin will show all created redirects in a list, hover over any item and click the “Check Redirect” link option. It will test the redirection and you should see a 301 status code is redirecting to a 200 status. This way, you can confirm the added redirect is correct and works properly.

Test Redirect in WordPress

You can also enable logging and setup the plugin to automatically add redirects when you change the URL under “Options” tab.

Setup Redirection Plugin

Although it is easier to setup and test redirects using Redirection plugin, the setup works only within your WordPress site. This means you can’t add redirect for a page on your subdomain or subdirectory located outside the WordPress installation. For example, you can’t add a 301 redirect from yoursite.com/page1 to blog.yoursite.com/page1 or vice versa.

2. Setup Redirect in WordPress with Rank Math Plugin

Though Redirection is a perfect plugin, most popular SEO plugins like Rank Math or Yoast SEO also offer a redirects manager feature. In this way, you can avoid using another plugin when you are already using a SEO plugin. Here, we will show the steps with Rank Math, which is free to use (it’s a premium feature in Yoast).

  • After installing and activating the free Rank Math plugin, first go to “Rank Math SEO > Dashboard” section and enable “Redirections” module.
  • After that, go to “Rank Math SEO > General Settings > Redirects” section. In this section, you can enable auto redirection type as 301 and enable “Auto Post Redirect” option. This will help to setup automatic 301 redirects whenever you change the URL of your posts.
Enable Automatic Redirection in Rank Math
  • If you want to manually add a redirect, go to “Rank Math SEO > Redirections” menu and click “Add New” button.
  • Provide the source URL, destination URL, and set the “Redirection Type” as “301 Permanent Move”.
  • Select the status as “Active” and click the “Save Changes” button for adding a redirect from your old page to a new page.
Create Redirection in Rank Math Plugin

You can view all the created redirects and deactivate an item anytime. Rank Math also works only within your WordPress installation and does not offer any option to test.

3. Using Your Theme’s Functions File

If you prefer coding within WordPress, you can add the 301 redirect code to your theme’s functions.php file. Remember to create a backup of your functions.php file before making any changes.

  • Go to your WordPress admin panel and navigate to “Appearance > Theme File Editor” section.
  • Select “Theme Functions” file (functions.php) of your active theme (recommended to use a child theme).
  • Add the following code at the bottom of the file and click “Update File” button. Make sure to replace the old-page and new-page with your relative URLs.
add_action('template_redirect', 'custom_redirect');
function custom_redirect() {
    $path = trim(parse_url(wp_unslash($_SERVER['REQUEST_URI']), PHP_URL_PATH), '/');
    if ($path === 'old-page') {
        wp_safe_redirect(home_url('/new-page/'), 301);
        exit;
    }
}

This is useful if you want to add redirects only for few pages within the WordPress setup. As parent theme changes will be wiped off when updating your theme, you should use another code snippet plugin or a child theme. Also, some themes do not come with functions.php file and you should anyway use other methods in that case. So, using a plugin is the best option for bulk redirects and to avoid editing the files.

4. Using .htaccess

If you are comfortable working with server files, you can add 301 redirects directly to your .htaccess file (with Apache servers):

  • Access your website’s files via FTP or through the File Manager in your hosting control panel.
  • Open the .htaccess file located in the root directory of your WordPress installation.
  • Add the following line of code at the end of the file and save the changes. Make sure to the old-page and new-page with correct URLs.
Redirect 301 /old-page/ https://www.yoursite.com/new-page/

This method is also suitable for adding a few items. Be careful when editing the .htaccess file, as incorrect changes can completely take down your site. The advantage is that you can add redirects for subdomains and subdirectories located outside the WordPress installation.

5. Using Redirect Manager from Hosting Panel

If you prefer not to install a plugin or editing files, then this is the best and safer option for you. Almost all hosting companies offer this feature regardless of whether they use the standard cPanel or custom hosting panel. For example, SiteGround uses a custom hosting panel (called Site Tools) and allows you to setup 301 redirects across your domains.

Here is how to setup redirects in cPanel:

  • Login to your hosting account and access the cPanel.
  • Search and open “Redirects”, you can generally find this under “Domains” section.
Redirects Manager in cPanel
  • Fill up the followings and click “Add” button.
    • Type: Select 301
    • https / www: Select the domain from the list
    • / – Add the slug part of your old URL (like old-page/)
    • Redirects to: Add the complete URL of the new page (like https://yourdomain.com/new-page/)
    • www. redirection: Select whether you want (generally, Redirect with or without www.)
    • Wild Card Redirect: Check this if you want to redirect all pages from one directory to another.
Create Redirects in cPanel
  • You can anytime view and edit all previously created redirects on the same page.

As you can see, this method gives the complete flexibility without writing any code. You can create redirects in the same domain, across two domains, from domain to subdomain, and in any other combination as long as all those domains are hosted on the same server. This option is generally suitable for site migration where you need to redirect the entire domain to another domain. The only problem is that you should access the cPanel every time to modify the redirects.

Tips for Beginners

  • Keep a List of Redirects: Maintain an organized list of all your redirects to avoid conflicts and keep track of changes over time. Both plugins and hosting panel options offer a list, other file editing options need manual management.
  • Check for Redirect Chains and Loops: Avoid redirect chains, where one URL redirects to another URL that redirects again, as well as redirect loops, where URLs continuously redirect between each other. Both can cause problems for users, search engines, and site performance.
  • Test Your Redirects: Use redirect-checking tools or browser extensions to verify that your 301 redirects are working correctly. If you are using the Redirection plugin, you can also use it to test and monitor your redirects.
  • Using Regex: This is a powerful tool for setting up conditional redirects. So, use it carefully and test to confirm it works as expected.
  • 301 and 302: Some methods explained above allows you to select the redirection type as 301 or 302. While 301 is used for permanent redirection, use 302 for a temporary setup.
  • Deleting Old URLs: Redirects will work regardless of whether you delete the old URLs or not. Before permanently deleting, make sure your 301 redirect is working. In case of 302, you should keep the old URL without deleting to restore it later when needed.

Summary

Here is a quick summary and comparison of the five methods, explaining where they work, when to use them, and their main limitations.

MethodWhere / When to UseBest ForMain AdvantageLimitation / Caution
Redirection PluginInside a WordPress installationFew to many redirects, including regex rulesEasy to create, manage, log, and test redirectsWorks within that WordPress installation
Rank Math RedirectsInside WordPress when Rank Math is already installedSites already using Rank Math for SEONo separate redirect plugin required; supports automatic redirects when URLs changeWorks within WordPress; redirect testing is limited
functions.phpInside a WordPress installationA few custom redirects when you are comfortable with PHPNo additional plugin required; complete control over the logicRequires coding; theme updates can overwrite changes unless using a child theme
.htaccessAt the Apache server levelA few redirects, server-level redirects, migrations, and redirects outside WordPressFaster than WordPress-level redirects and can handle URLs outside the WordPress installationIncorrect changes can cause server/site problems; requires server-file access
Hosting Panel Redirect ManagerAt the hosting server levelRedirects between domains, subdomains, directories, and site migrationsEasy to configure without coding; can handle redirects outside WordPressRequires access to the hosting panel; options vary by hosting provider

Leave a Comment

Your email address will not be published. Required fields are marked *