What is 301 Redirect and How to Configure it on WordPress

301 Redirect

301 Redirect is forwarding. It is used to redirect users to another URL (page or site entry) different from the one they originally requested. For example, if a person in the address bar is writing your-site.com when properly configured, it will go to www.your-site.com or vice versa. 

Besides, we will help you implement these redirections while retaining your existing traffic and SEO.

Five Types of URL Redirects

  • 301 – Permanent redirect. Used to tell search engines that the page has forever changed its location. In 99.44% of cases, you will use this type.
  • 302 – Temporary redirection. Used to tell search engines that the page has changed its location at the moment not forever.
  • 303 – See Other redirect. Sometimes it is used in online stores to check if pages with one-time data were added to tabs or simply updated. This response code tells the browser that the requested document needs to be requested by the GET method, even if the original query was performed by another method. This is the response code entered to replace the 302 code in the HTTP / 1.1 protocol.
  • 307 – “The requested document is available for a short time on a different URI”, tells the browser that the requested document is required to be requested by the method (GET / POST) that the primary request was made to, changing the query method is prohibited. This response code is also entered to replace the 302 code in the HTTP / 1.1 protocol. Temporary Redirect. It works like a 303 redirect but with some changes in the way information is transferred from one page to another.
  • 308 – “The requested document was finally migrated to a new URI”, this response code was added to HTTP / 1.1 instead of code 301, in addition it means that the request method (GET / POST) is not allowed to change when a new URI is migrated. If you requested a resource in the first query using the GET method, then the second query must also be performed using the GET method.

Most often for redirection, the answer code is 301, so redirection is also called the 301 redirect. All examples in this article will use this response code. You can configure 301 redirection in different ways: PHP, JS, .htaccess, and also using special plugins.

We’ll look at how to do this with the Redirection plugin, as well as editing the .htaccess file.

WordPress Redirection Plugin

The process of manually adding redirects to the site can be quite complicated, since in this case it will be necessary to edit the .htaccess file. But in the WordPress world there is a fantastic Redirection plugin from John Godley which can handle redirects right in the WordPress console.

This plugin has been on the market for a long time already and is popular due to the fact that it is very easy to import an address list into a table, so you do not have to manually enter all redirects.

To install the plugin, simply enter “redirection” in the search in the Plugins → Add new in your WordPress console.

After activation, you can find the plugin menu via Tools → Redirection.

To manually create a redirect, simply enter the source URL and the new destination URL in the appropriate fields, and then click the “Add Redirect” button. This is an excellent and simple method when you need to make just a few changes.

Add a 301 Redirect by Editing a .htaccess File

In the hosting management section, the data for connecting to your site files via FTP is specified. To connect, you need to use an ftp client, for example FileZilla.
After connecting, we begin editing the .htaccess file. It is located in the root directory of your site.

For editing it is necessary to use such code editor in which it is possible to specify the encoding and format of the end of lines, for example, Notepad ++.

Examples of redirection:

HTTP to HTTPS Redirection

Redirect http://example.com to https://example.com, including all website links:
After connecting the ssl-certificate, you should add the mandatory redirect from http to https, so that all your connections are encrypted. Such a redirect will exclude the possibility of connecting via the http protocol.

To do this, insert the following code in the very beginning of the .htaccess file:

# BEGIN Redirect
RewriteEngine On
RewriteCond% {HTTP: X-Forwarded-Proto}! Https
RewriteRule ^ (. *) $ Https: //% {HTTP_HOST}% {REQUEST_URI} [L, R = 301] 

# END Redirect

WWW to non WWW Redirection

Redirect from www.example.com to example.com, including links:

# BEGIN Redirect
RewriteEngine On
RewriteBase /
RewriteCond% {HTTP_HOST} ^ www. (. *) $ [NC] RewriteRule ^ (. *) $ Http: //% 1 / $ 1 [R = 301, L] 

# END Redirect

Note, if you have an ssl-certificate attached, the line:

RewriteRule ^ (. *) $ Http: //% 1 / $ 1 [R = 301, L]

It is worth changing to:

RewriteRule ^ (. *) $ Https: //% 1 / $ 1 [R = 301, L]

non WWW to WWW Redirection

# BEGIN Redirect
RewriteEngine On
RewriteCond% {HTTP_HOST}! ^ Www.
RewriteRule ^ (. *) $ Http: //www.% {HTTP_HOST} / $ 1 [R = 301, L]

 # END Redirect

 

Note, if you have an ssl-certificate attached, the line:

RewriteRule ^ (. *) $ Http: //www.% {HTTP_HOST} / $ 1 [R = 301, L] 

It is worth changing to:

RewriteRule ^ (. *) $ Https: //www.% {HTTP_HOST} / $ 1 [R = 301, L] 

When setting up this redirect, you need to change the name of your site in the database by adding www. You need to do this in phpMyAdmin by editing the lines
siteurl and home in the wp_options table (if the table prefix in your database is “wp_”).

Using Redirect at .htaccess File

If you want to use several of the above redirects, you should write them in the following order:

Redirect from www.example.com to example.com or Redirect example.com to www.example.com.

Redirecting http://example.com to https://example.com.

There are many other rules, in particular, using regular expressions. But they are individual and not needed as often as those described above and their description is beyond the scope of this article.

If you need to write more complex redirection rules, we recommend that you look at the documentation on the apache 2 web server and its mod_rewrite module, as well as on the regular expressions of the PCRE format:

https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
https://httpd.apache.org/docs/2.4/rewrite/
http://pcre.org/current/doc/html/

FURTHER READING