Uncategorized

What is a 301 Redirect? Seven Things to Know

A 301 redirect is just like a mail forwarding service. It tells your website's visitors how to find the new address for a page they're looking for at an old (and now incorrect) address.

Dan Knauss

Imagine checking your mailbox for days after you move to a new address, but there’s never anything there. Not even junk mail! Oops — you forgot to tell the postal service your address changed! All your mail has been returned or held at the post office. That happens to a website’s visitors — and customers — when they reach a missing page and see a “404 Not Found” error screen in their browser. This is when 301 redirects come to your rescue.

A 301 redirect is just like a mail forwarding service that tells your website’s visitors how to find the new address for a page they’re looking for at an old (and now incorrect) address.

In this guide to 301 redirects, we’ll cover everything you need to know about 301 redirects for your WordPress website, including seven things you need to know about 301 redirects and how to create a 301 redirect in WordPress.

What is a 301 Redirect?

What is a 301 redirect? In a nutshell, a 301 redirect takes a user from an old URL to a new URL that replaces the old one that no longer exists.

Specifically, the 301 HTTP (HyperText Transfer Protocol) redirect status code indicates the content at the URL the user tried to access has been permanently moved to a new URL.

With a 301 redirect, your browser will redirect itself from the old URL if you go there. You’ll automatically be sent to the new URL where the content is now. Unless you have configured your browser to block redirects (high-security configurations may do this), you’ll end up at the updated URL when browsing the old one.

A WordPress site needs to use 301 redirects after undergoing significant changes to its domain and/or URL structure. Let’s look at the mechanics behind these redirects, learn how to create a 301 redirect in WordPress, and consider the cases where you should use 301 redirects.

Get SolidWP tips direct in your inbox

Sign up

This field is for validation purposes and should be left unchanged.
Placeholder text
Placeholder text
Thanks

Oops something went wrong, please try submitting again

Get started with confidence — risk free, guaranteed

7 Things to Know About 301 Redirects

To understand 301 redirects, there are a few important (technical and non-technical) things you need to know.

  1. Plan to create a 301 redirect any time you delete a post or page on your website or blog. Whenever you delete a post or page on your website or blog, you must consider which post or page should replace it. That’s where 301 redirects come in.
  2. A 301 redirect helps you avoid 404 “Page Not Found” errors. A 404 error occurs when you delete a WordPress post or page and don’t create a 301 redirect to a relevant location. 404 errors should be avoided at all costs.
  3. Major search engines recommend that you immediately add a 301 redirect when changing the URL of a page or post. This way, their search engines can track the changes and keep their results to your site up-to-date.
  4. There are highly technical ways to create 301 redirects, or you can use a tool like a WordPress plugin to simplify the process. (We’ll explain more about this later.)
  5. 301 redirects should only be used if the content at the URL is public and not due to be changed anytime soon. Getting this wrong can also impact your SEO rankings, so watch out.
  6. Beware of having too many 301 redirects. You can get penalized with lower search result placement if multiple redirects are stacked up on each other, which increases the time it takes to find and load the correct page content. For example, if a URL has changed several times, you might have a 301 redirect for each change. Oops!
  7. Generally, when a browser receives a 301 response, bookmarks will be updated to the new URL automatically. That’s good news.

Using 301 Redirects to Secure and Simplify Your Site for Users

Here are a few common technical benefits of 301 redirects that are often used to make sites more secure and user-friendly.

  • 301 redirects can eliminate the www. in a web address. Servers are often configured to insert a www. if a user or link doesn’t supply it. It’s an unnecessary extra piece of clutter in the address bar, so why not remove it? If someone types in the www. they’ll be redirected to the “naked” domain without the www.Search engines want you to consistently use a “canonical” domain/address, which should always omit the www.— if you decide to take that route.
  • A 301 redirect can force unencrypted http:// connections to switch to the SSL-encrypted https://. Your WordPress site should use SSL encryption by default, so all connections come over HTTPS — not HTTP. Good hosts will ensure you use SSL, but some WordPress plugins, themes, or code on your site might try to load assets over HTTP. This creates a potential security vulnerability, and many browsers will display a warning about it or even prevent your site from loading. You can set up 301 redirects to ensure any HTTP connections are automatically changed to HTTPS. Then, everyone visiting your website is secure even when they use an old, unencrypted HTTP URL.

How Do I Create a 301 Redirect in WordPress?

There are two primary methods to create a 301 redirect in WordPress:

  • Create a 301 redirect in WordPress manually. If you’re comfortable with the more technical realm of website files and server configurations, this method is for you. If not, you’ll need a plugin.
  • Use a WordPress redirection plugin. This approach may be preferred by anyone managing a complex, commercial site or multiple sites. Plugins can automate the process and perform it across all sites you own.

First, we’ll look at how you can manually make a 301 redirect for your WordPress site. To make a clear and concise tutorial, we will make some presumptions about how you’re operating your site. These are outlined in the next section. Plugins can automate the process of 301 Redirect and perform it across all sites you own.

Second, we’ll examine how you might use plugins to perform this operation. The manual method may seem simple enough, but here are some reasons why no WordPress site administrator should manually make their own 301 redirects for each site they manage:

  • Best practices make it clear that all changes across sites should be uniform. There will invariably be minor differences if the redirect is manually made for each site you manage.
  • As you’ll see, 301 redirects can turn from simple to complex pretty fast. When copying and pasting code, you still might run into text encoding issues (depending on the Operating System used), as well as other issues.
  • Over time, you’ll need to make new updates. Memorizing this syntax would be nearly impossible. A plugin keeps everything consistent over the years and through your growth!
WARNING: Before you make major changes, Use a WordPress backup plugin to save all the data and the current state of your site. Though very unlikely, it’s possible you could mistakenly make your site inaccessible. A backup ensures you can restore your site to its former glory with a single click!

Create 301 Redirects with a WordPress Plugin

A WordPress plugin offers a simpler solution for your 301 redirect needs. These plugins will automate the process.

NOTE: Be sure to choose a WordPress redirection plugin that has a solid history of development and support. You don’t want to be left with an abandoned plugin that is no longer updated or maintained.

Redirection Plugin

The Redirection plugin allows you to manage your 301 redirections and keep track of 404 errors so you can fix them with 301 redirects. It’s a handy plugin and one we use and recommend.

Easy Redirect Manager Plugin

The Easy Redirect Manager can also accomplish 301 redirects for you. Upon installation, you’ll just need to open it. You should see a table of links and be able to add any redirect with any status code applicable. Simply pick an old URL, enter the new URL with it, and ensure it’s marked as a 301 redirect.

Simple 301 Redirects Plugin

Simple 301 Redirects is an even more focused plugin. Solely intended for those looking to perform 301 redirects, it does more than just preserve the redirect. It’ll also ensure that those concerned about SEO rank won’t lose what they already have by making a 301 redirect for their WordPress site.

SEO Plugins Like Yoast SEO

Many WordPress SEO Plugins, like Yoast SEO, manage 301 redirects as a premium feature within the plugin since they are vital to your site’s search rankings. Check to see if your WordPress SEO plugin has 301 redirect management as a built-in feature in either their free or paid version.

Create 301 Redirects Without a WordPress Plugin

Next, we’ll review making a 301 redirect without a plugin. In our first example, we’ll be covering how to make one URL redirect to another page on your site. Later, we’ll show you how to ensure everyone accessing your site has a secure connection. This is also known as “forcing SSL.”

We’ll be using the built-in WordPress PHP function called wp_redirect.

NOTE: By default, the wp_redirect function calls HTTP status code 302 (temporary), not 301 (permanent). That's why one parameter in our function will be "301." If we omit the specific status code, WordPress assumes we want a 302 redirect!

A 302 redirect means "temporarily moved" and tells search engines not to update your search result listings. This would ultimately be detrimental to any SEO strategy you have in place now or in the future.

In our example, let's say you have a site called our-example-wp-site.com. Let's assume a page on a URL — our-example-wp-site.com/old-url — needs to redirect to our-example-wp-site.com/new-url. Because we have WordPress installed, we don't have to use raw PHP and can instead use that wp_redirect function.

Using the wp_redirect Function

Using a file manager, navigate to the file containing the contents of our "old-url" page. Open this file to edit and replace all the contents (other than the beginning and ending markers used by PHP) with this single line of code:

wp_redirect( "http://www.our-example-wp-site.com/new-url" , 301 );

As you can see, it's a simple WordPress function with two parameters. The first parameter is the link to our old URL's page. The second parameter is technically optional, but if left blank, it will default to an HTTP status 302, which we don't want.

Using PHP Code

By far, wp_redirect is the simplest WordPress-specific way to manage a single-page redirect. But if you have content outside WordPress, like a static PHP+HTML page, you can't use wp_redirect. You can use pure PHP, however. 

To start, we'll need to edit the file you want to redirect. This should be the file that contains the contents of the old URL that we need to redirect to a new URL.

Once again, let's assume that we have the domain our-example-wp-site.com and we want to redirect traffic from our-example-wp-site.com/old-url.php to our-example-wp-site.com/new-url.php. Here's the code that should go in the old-url.php file in plain PHP:

 <?
 header( "HTTP/1.1 301 Moved Permanently" );
 header( "Location: http://www.our-example-wp-site.com/new-url.php" );
 ?> 

The first line of this code is the exact HTTP status response to send to any visitor's browser. Note that the prefix of HTTP/1.1 must be exactly as written. The next item must be the number of the status code. The portion that says "Moved Permanently" must be the correct corresponding string of words for our HTTP status code.

It gets tricky here. Technically, each item is a parameter in the string that the client's browser reads. They are all manually typed in, but they must be exactly correct. Otherwise, the client will get a "bad header" error.

The second line in our PHP code must be the location of the new URL. This must contain the full path of the URL. In other words, we must include the entire URL, even if it's a file within our site. The reason is that it's sent as the next browser header to the client, and it will be interpreted as it's written. Since the client won't have your site's files locally, it needs a reference to the complete, updated URL.

301 Redirect WordPress .htaccess

Whether you're using WordPress as your content management system or not, if you're using the Apache (or Litespeed) HTTP server, you can use the .htaccess file to make a 301 redirect happen.

The .htaccess file will be on your server in the "web root." That is, it'll be in the top directory that holds your site's public HTML content. (It's often named the public_html folder.) When adding anything to your .htaccess file, you should always back it up first. The .htaccess file can get very long and complex. Another rule of thumb with these files is always to append your changes, which means always adding new lines to the end of the file.

First, locate your .htaccess file and back it up on your local machine. Once you've done that, you can open the live version on your server in a text editor. Go to the bottom of the file. In this example, we're going to make sure that your site works regardless of whether a user enters www. before your URL or not.

The .htaccess 301 Redirect Code

Remember that the .htaccess file is spacing-sensitive. Put one space between each item you type in. We recommend typing rather than copying and pasting since these files are notorious for encoding errors due to copying and pasting. First, we'll show you the code. Second, we'll explain what it means:

 RewriteEngine on
 RewriteBase /
 rewritecond %{http_host} ^our-example-wp-site.com [nc]
 rewriterule ^(.*)$ http://www.our-example-wp-site.com/$1 [r=301,nc] 

In our first line, we're turning on the Apache RewriteEngine. This essentially is booting up the component that allows us to rewrite the content and URL the client will see. The second line has us setting up from where we'll rewrite. By putting "/" as the base, we're telling our server that we want everything beginning at our webroot to be rewritten.

Analyzing Our .htaccess 301 Redirect Code

The rewritecond in our third line defines a "trigger" with the conditions that must be met to rewrite our URL. The %{http_host} portion will be triggered on any attempted HTTP/S connection to our site. The caret (^) before our domain means the trigger is set regardless of whether a user has already added www or is using another subdomain. At the end, you'll notice we finished with [nc]. This is Apache shorthand for "no case," which tells the browser it shouldn't care whether any part of the URL is upper or lowercase. So, this trigger will apply to any user agent accessing any URL on our site over HTTP/S.

The last line says what .htaccess should do when the conditions defined in the third line are met. Meeting those conditions triggers the following rule:

rewriterule ^(.*)$ http://www.our-example-wp-site.com/$1 [r=301,nc]

If it looks like a foreign language, that's because it uses "regular expressions" or "regex." There are often entire semester Computer Science courses in regex. It's a powerful and complex tool.

Regular expressions are used to format and rewrite all kinds of data. Essentially, this line uses regex to preserve all of the URL, adding our www. before it, and the $1 within the URL is saying to keep everything after the .com in our domain:

At the end of that last line, you'll see we have [r=301,nc]. The first parameter in brackets, r=301 uses Apache shorthand again. The r is short for "Redirect HTTP response code," and we're indicating it should be a 301 redirect. Our second parameter, nc, is used again to indicate that we're not concerned about whether characters are upper or lower case.

Testing Our .htaccess Changes

Once you save and run this, try deleting your temporary files and cache by going to your browser and pressing Ctrl+Shift+Del. Select everything, then press either "Apply" or "OK," depending on which browser you're using.

Try going to your domain without www. in it. Your browser should insert www. for you now. Keep in mind that we're inserting http://www. ... If you have a valid SSL certificate installed, you'll need to force SSL as well. This is as simple as adding a single 's', and changing what is inserted before our URL to https://www.

Wrapping Up: 301 Redirects and You

Nothing frustrates readers or search bots more than roadblocks. Frustrated readers don't hang around and often don't return.

It’s important to keep your readers moving to active pages if you want to keep your readers reading on your website. Implementing a plan to use 301 Redirects in WordPress benefits the readers, the search engines, and you.

Did you like this article? Spread the word: