How to set up 301 redirects in WordPress

Don’t leave your visitors hanging

A 301 redirect is a permanent redirection from one website page to another. The code 301 is web server speak for “moved permanently.”

When a browser receives HTTP status code 301 in response to a page request, it knows to transfer the user to an alternate page, different from the one they initially linked to, and that furthermore, this is a permanent change.

Setting up these redirections is particularly useful if:

  • You redesign your site and some URLs change.
  • You rename a post (sometimes WordPress will handle this for you but not always).
  • You are transferring your site to HTTPS/SSL.
  • You change your permalink or slug structure.

Redirects ensure the visitor gets to the appropriate page and avoids an annoying “Page not found” message. A redirect also helps transfer the old page’s SEO juice and backlinks to the new page.

Related: Best practices for protecting your website SEO in a redesign

This article reviews different ways WordPress admins can set up 301 redirects. The most common methods are:

  • Through a WordPress plugin.
  • By editing the .htaccess file.
  • Via PHP modifications.

Related: How to fix a 404 error

WordPress 301 redirect plugins

The most painless way to add 301 redirects to a WordPress installation is via a plugin. There are many free plugins that will handle this for you. Two top options include:

Redirection by John Godfrey

301 Redirects WordPress Redirection

This free Redirection plugin has more than 1 million active installations for good reason — it’s simple and does the job well. It handles everything from basic redirects to redirects based on particular conditions, such as the user’s login status, browser type, referrer, or other conditions. This plugin will also track all 404 (Page not found) errors on your site.

Once you install and activate it (Plugins > Add New), it will be located under Tools > Redirection. Adding a new redirection is as simple as entering a source and a target URL.

301 Redirects WordPress Add New

Simple 301 Redirects

301 Redirects WordPress Simple

Simple 301 Redirects is another great option. It doesn’t offer as many options as Redirection but it’s simple and works smoothly. Once you install and activate it (Plugins > Add New), it will be located under Settings > 301 Redirects. From there, you can easily add source and target URLs.

301 Redirects WordPress Simple 301 Redirects

WordPress 301 redirects via .htaccess

Every plugin adds a little more overhead to WordPress, so some admins opt to handle redirects through their .htaccess file instead. For the full scoop on .htaccess, see our .htaccess tutorial.

In brief, .htaccess is a plain text file you can use to make configuration changes to the Apache web server that delivers your pages. If your hosting isn’t Apache, you may not be able to use .htaccess.

The .htaccess file is usually located in the root of the website and can be modified with a basic redirect command.

A website can have more than one .htaccess file, but there’s usually at least one located in the root of the website. You can modify it with any text editor, including through cPanel File Manager’s editing capability. A basic redirect command takes the following format:

redirect [status] [URL-path] URL

 

.htaccess redirect examples

Here are a few examples covering common redirection scenarios.

Redirect to a page on the same host:

redirect 301 /oldpage.html /newpage.html

Redirect to a page on a different host:

redirect 301 /oldpage.html https://www.example.com/newpage.html

Redirect entire WordPress site to a new site:

redirect 301 / https://www.yournewsite.com/

You can line up as many of these as you need in your .htaccess file.

Editing .htaccess through cPanel

The ins and outs of editing .htaccess are covered in our .htaccess tutorial but here’s a quick recap:

1. Log into cPanel.
2. Under the Files section and click on the File Manager icon.

301 Redirects WordPress File Manager

3. In the pop-up window, select the Document Root for the site you are working on and make sure Show Hidden Files is checked. Note that some versions of File Manager might have a slightly different interface but will require the same setting selections. If the below window doesn’t pop up automatically when you click on File Manager, look for a Settings icon and click on that to reach these settings.

301 Redirects WordPress Show Hidden Files

4. Look for the .htaccess file and right click on it. From the drop-down menu that appears, select Edit.

301 Redirects WordPress Edit

You will now be in the text editor, where you can make and save changes.

Setting up WordPress 301 redirects with PHP

If you prefer to add redirects without using a plugin or altering your .htaccess, you can opt to manually code the redirects into the PHP of your WordPress theme.

This is the most advanced option. While you could add code directly to the header.php file, it’s better practice to place it in the functions.php file and use a WordPress hook. This way you aren’t altering core files and you can keep your modifications in one place.

You should also use a WordPress child theme to make your edits. Otherwise if you update your theme, your changes will be overwritten and lost.

wp_redirect syntax

The WordPress PHP code to redirect a page is structured like this:

wp_redirect( ‘http://mysite.com/mynewpost’, 301 );
exit;

 

http://mysite.com/mynewpost is the URL you want to redirect to. 301 is the status code to use. The exit statement is required.

Since the code in functions.php gets executed on every page, you’ll need to wrap the redirect in a conditional requirement if you only want to redirect a specific URL. For example:

if($_SERVER['REQUEST_URI']=="/somedir/some_page_you_want_to_redirect")
{
wp_redirect( ‘http://mysite.com/mynewpost’, 301 );
exit;
}

 

Note that the request URI will be everything after your domain name. So in the example below, the request URI string to use would be everything in bold:

http://mysite.com/somedir/somepost

How to edit your functions.php file in WordPress

Editor’s note: Editing theme files via /wp-admin/ is risky. Bad code could prevent WordPress from loading, which then prevents you from fixing the code. We recommend making edits via SFTP on a staging site. One-click staging sites are a feature in our Managed WordPress hosting plans.

You can add these changes to functions.php through the Admin control panel. From the Admin dashboard select Appearance, then from the submenu, select Editor. The right side of the screen will list files you can edit. Select Theme Functions (functions.php).

301 Redirects WordPress Edit Themes

Scroll to the bottom of the file and insert your code snippet. Then click the “Update File” button to save your changes.

301 Redirects WordPress Update File

Visit the old page and confirm that the redirection is working.

Wrapping up

There are even more ways to add redirects to your website. For example, if you moved your entire site, you could automatically forward visitors from your old domain by adding the below to the header.php file of your theme on the old site:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.yournewwebsite.com");
?>

 

Which route you choose is a matter of preference. The plugins offer a quick and easy interface that many people prefer. However, if you don’t want to add more plugins to your site, .htaccess might be the best route for you. Modifying your site’s PHP files directly should be reserved for advanced users.

You can always check if a redirect is working by going directly to that page and seeing where you end up.

 

If a new redirect doesn’t work, and you previously visited the page, try refreshing your browser cache as the page may be loading from memory. You can also use an HTTP status code checker to find out exactly which status codes your site is returning for any URL.

Taking the time to set up 301 redirects in WordPress is definitely worthwhile. You’ll elevate your site’s user experience and keep old, dead links from negatively affecting that search engine ranking you’ve worked so hard to achieve.

Image by: Michel_Rathwell on Visualhunt