Published by on January 29, 2020 • 0 Comments • Lire en Français

Redirects: believe me, I would have been delighted to know what that word meant when I discovered WordPress a few years ago.

Except that, when I opened my first blog, I didn't know anything about it.

Someone installed and uploaded a brand new WordPress for me. A blank page, which I hastened to fill in, fill in, fill in. I published article after article, while discovering how rich the web is.

After a year of blogging, noticing few results in terms of traffic, I started to be interested in SEO and… boom!

I realized that I had made a lot of mistakes. Mistakes, or even disasters, I might say.

The worst of them all (I'll spare you the others, they're irrelevant)?

More importantly, I had to set up hundreds of redirects.

By hand.

redirection mode d'emploi facile

But after that, my blog started to appear in Google search results, and even to rank quite well.

This unpleasant experience helped me understand the importance of redirecting links. If this word doesn't mean much to you, don't worry, I'll explain everything in this article. And in the end, you'll be a pro!

What is a redirect?

A redirect automatically redirects a visitor that wants to access URL A (ex: https://mysuperwebsite.com) to URL B (ex: https://myawesomewebsite.com).

You could say you “force” him to take an alternate route, without this having any impact on his travel time. He's just not going to realize it. Let's just say that the initial route he wanted to take has turned into a dead end.

The most common redirect is 301 redirect, known as permanent redirect. This article focuses primarily on it, as you will be using it in most cases.

A 301 redirect lets Internet users and search engines know that content has been permanently moved or deleted.

This allows your new URL to retain the notoriety of the old one. If you redirect a popular and good-ranking page to another page, the latter will (in theory) keep an equivalent ranking and traffic.

So this is the type of redirect I used for my blog: I redirected all my URLs

The old URL with my old permalink format

to my new URL format

The new, redirected URL

so that Google will forget about these mistakes once and for all!

Some redirects are less frequent, but you can also meet them on your WordPress journey:

  • 302 Redirect: redirects a primary URL to a secondary URL temporarily. Search engines continue to index the main URL. This type of redirect can be used for sites in maintenance, for instance.
  • Redirects 307 and 308: reserved for technical cases. We won't bother to detail what they are, because you won't have to use them.

When are redirects required?

Here is what Daniel Roch, WordPress SEO expert at the French SEOMix agency, recommends.

“Whenever you are going to delete or change the URL of a post, you'll need to create a redirect. This will avoid displaying a useless error page by displaying the most relevant content, while keeping the links that pointed to it. In other words, it improves the navigation on your website while allowing you to keep your ranking.”

To make it clearer, you will need to redirect if :

  • you decide to update your posts' or articles' slugs.
  • you modify or delete any type of content on your site (media, pages, posts, products, portfolios, etc.).
  • you edit or delete categories, tags, authors.
  • you change your domain name.
  • you change your permalinks' structure on your website.

Setting up redirects may seem quite complex. Wondering if you really have to do it?

Spoiler alert: the answer is YES!

Otherwise, you risk facing the ultimate punishment: the 404 error. This refers to a page that does not exist or no longer exists. And Google doesn't like 404 errors. Nor your visitors. Nor you. Yes, indeed, nobody likes them.

Redirection : supprimez les erreurs 404
Here's your website, if you leave too many errors 404 going.

Redirects also help improve User Experience on your website, providing smooth navigation for your visitors, and preventing them from landing on error pages.

No matter how much you create nice and humorous 404 pages, it's always frustrating to come across them, and they can scare your visitors away!

An example of a cool 404 page

Are you still there? Are you ready to pamper Google and your visitors? Then let's find out how!

The Redirection plugin

With 900,000 active installations, Redirection is the most popular WordPress redirect plugin in the official directory 🙂

Easy to use, intuitive, and quick to set up, it allows you to create redirects without typing in a single line of code.

If you are a WordPress beginner and want to redirect a page to another, you'll love it. Thanks to it, you will be able, among other things, to:

  • create different types of WordPress redirects ;
  • manage the 404 errors on your website;
  • import or export redirects ;
  • create groups to organize them.

Looks quite interesting, right?

How to set up a simple redirect

Download the Redirection plugin, then install and activate it on your WordPress site.

In the Tools tab of your dashboard a sub-tab Redirection has appeared.

Simply redirect URLs in WordPress

It takes you to the page where you can manage your redirects.

To set up a redirect, just fill in the Source URL (the one you want to replace), and the Target URL (the one you want it to lead the visitor to).

Click the Add Redirect button : here, you have created your first WordPress redirect.

However, you can go even further.

By clicking on the wheel icon, you'll see additional options:

WordPress redirects made easy with the Redirection plugin

You can choose where you want to redirect your URL to (another specific URL, a random post, your 404 page), as well as the type of redirect you want (permanent or temporary).

Remember that in 99% of the cases, you will have to do permanent redirections (the so-called 301 redirect).

You can choose the group in which to classify the redirect. By default, the plugin creates a Redirections group and a Modified Posts group.

But you can create others in the “Groups” tab, for example if you want to classify your redirections according to the headings of your site.

Finally, the “Position” field allows you to specify a priority for when the redirect is executed. The smaller the number, the higher the priority. I advise you not to touch this field: the plugin handles it very well by itself.

Info: Once your WordPress redirect is created, remember to empty your cache if you use a cache plugin, so that the change is effective. Most of the time, it won't be necessary – but it's better to plan ahead!

In the Redirects tab, you can access your redirects list, and find several interesting information:

Your list of redirects in WordPress
  • Code is the type of redirect you have set up.
  • URL indicates the source URL (the old URL) and the new one below it (the target).
  • Hits details the number of times the redirection has been triggered.
  • Last Access reveals the last time it was followed by a visitor.

From this list, you can modify, deactivate or delete a redirect (one by one or in bulk): as with post or WordPress pages, the parameters appear when you hover over the relevant line.

Easy, isn't it? Come on, let's move on!

How to set up an advanced redirect using regular expressions

Now that we've warmed up, let's go a little further.

The Redirection plugin also allows you to set up more advanced redirects, thanks to regular expressions (Regex).

While these allow you to create simple redirects, as explained just before, they will mostly help you change several URLs from a single redirect.

I suggest you find out how with the help of a case study.

Let's say you have a recipe blog.

You have created a “Desserts” category, but for a reason of your own, you decide to rename it “Sweet”.

If you have chosen a permalink structure like https://your-website.com/category/post-title (which is not a good idea), all your posts' URLs will be modified, and will therefore send your visitors to 404 pages.

If you are up for it, and have time to waste, you can create all of your posts' redirects by hand. But there indeed is a much faster solution. Yep, you got it: regular expressions.

You can create a regular expression that transforms all URLs looking like /desserts/post-title into URLs like /sweet/post-title.

So how does this work?

In the Source URL field, specify: https://your-website.com/desserts/(.*), and don't forget to check the Regex box – otherwise your regular expression won't work.

In Target URL specify:https://your-website/sweet/$1 .

Like this:

An advanced redirect in WordPress

You could also write it like this:

  • Source URL: ^/desserts/(.*)
  • Target URL: /sweet/$1

These two lines actually correspond to a Find/Replace command.

The first line allows you to search for all expressions that begin with https://your-website.com/desserts/, regardless of the characters behind this. The second line indicates that they should be replaced by this expression https://your-website.com/sweet/.

Go ahead and click “Add Redirect”! All your posts' URLs are automatically modified. Quick and easy, right?

Regular expressions are a tricky little exercise to pick up. But, once you master them, they allow you to create complex and powerful redirect!

Here are some resources if you want to learn more about this topic.

How to organize your redirects using groups

Groups allow you to organize your WordPress redirects into categories. When you have hundreds of them to create, it becomes really important to know where everything is located.

Click on the “Groups” tab. You'll notice that the Redirection plugin has created two o those, by default :

  • Modified Posts
  • Redirections

You can add your own group below. For example, “Extensions”.

Redirect groups

Via a small drop-down menu, you can either select :

  • WordPress: this creates redirects from your WordPress. By default, I advise you to stay on this setting.
  • Apache: this sends the redirects to your server, adding them to your .htaccess file.
  • Nginx: this is meant to create Nginx (a web server software) redirects. In this case, your server has to be running with it for it to work. Most of the time, servers run on Apache, so you won't need to use it.

When you set up a redirect, from the “Redirects” tab, you can assign it your new group, if you wish.

Redirect groups in WordPress

How to “follow” your redirects

If you are the kind of person who likes to know everything, it might be interesting to go to the “Log” tab. There, you'll discover all the redirects followed by your visitors.

The Log tab in the Redirection plugin

There's a lot of information in there that can be interesting to take a look at, from time to time. You'll have access to the following data:

  • The URL visited
  • The target URL to which the visitor was redirected
  • The redirect's date and time
  • Your visitor's browser
  • Their IP address

Note that by clicking on certain options (e.g. IP address or browser), you have access to more detailed information, such as the IP address' geolocation.

How to track your 404 pages

Changing URLs, updating your permalinks, or triggering errors when creating your links: all of this can lead to terrible 404 errors.

These are the first and foremost reason why you have set up redirects. The Redirection plugin allows you to track and delete them.

To do this, go to “404s” tab. You'll find the list of 404 errors triggered on your website, and will be able to correct them directly by clicking on Add a redirect, under the concerned error.

Remember to visit this tab regularly, to correct your errors and delete lines to lighten your database.

However, stay cautious: not all errors need to be corrected. You will sometimes encounter unknown and mysterious URLs. Most of the time, these are robots trying to access unavailable resources.

It's perfectly normal and there's not much you can do about it. It is up to you to determine which errors are true 404s and need to be corrected.

Here's a hint: this must concern posts, pages or other content on your site 🙂

And since it's better to be safe than sorry, I also advise you to track the presence of 404 pages using Google's Search Console.

How to import/export your redirects

Thanks to the “Import/Export” tab, the Redirection plugin also allows the import and export of redirects. This can be useful if you have a lot of redirections to set up at once.

Import and export redirects in the plugin

For your information, you can import and export CSV (e.g. Excel), Nginx, JSON, Apache .htaccess files.

Note: CSV stands for “comma-separated values”. It's a spreadsheet file where values are separated by commas. For JSON it's a file type that allows to store text data.

The dirty little secrets of the Options tab

As its name implies, the “Options” tab provides access to additional settings.

If you're feeling generous, you can make a donation to contribute to the plugin's development: it's a good way to support a tool when it's useful.

You can also define a how long you want to keep redirect and 404 logs for.

As soon as a user (or robot) triggers a redirect or lands on a 404 page, the plugin stores it in its logs (“Logs” tab for redirects and “404s” tab for 404 errors).

These logs can be useful, and it is important to consult them. But you'll have to purge them regularly to de-clutter your database and prevent it from growing unnecessarily large.

You can, for example, set the deletion of logs every month: this gives you time to analyze them and correct errors, without making your database heavy.

The most interesting part remains the possibility to automatically create redirects as soon as you change a content's slug. This option is absolutely essential to activate.

To do this, select the appropriate content type. Let's take pages and posts, for example. Check the corresponding box as shown in the capture below.

URL monitor settings in the WordPress Redirection plugin

Finish by associating a group with your future redirects… and that's it.

The “Options” tab also allows you to set a time limit for Redirect Cache.

This means that, when a visitor encounters a redirect, his browser remembers it. It does not have to perform the query every time the user tries to access the source URL.

The user's navigation is therefore faster and more fluid.

But if the browser caches a redirect, and you change it in the meantime, this change will not be taken into account by your user's browser.

It will still display the old redirect, which can be problematic for you and the user.

The change will only be taken into account once the caching has expired.

So if you change your redirects often, you can set a low caching time (e.g. one day).

If, on the other hand, you only make changes in your redirects very rarely, you can set a higher caching duration (for example, one week).

Support tab

The “Support” tab allows you to check that everything is working properly. If all lights are green, it means that the Redirection plugin is correctly setup and operational.

Support tab

This tab also gives you access to the documentation, FAQ and source code of the the plugin.

Is everything okay? Let's switch to a new method for creating redirects. A method that is a little more frightening, especially for beginners.

But don't panic: I'm going to give you all the keys to handle this explosive safely.

The classic WordPress redirect: .htaccess

Although efficient and useful, WordPress Redirection via .htaccess doesn't come without risks.

With it, you're going to have to get your hands dirty by manipulating the famous .htaccess file, which is located at the root of your WordPress site.

This is a configuration file for Apache, one of the software used by your web host to run its servers.

The contents of the .htaccess file will send instructions to the server to :

  • improve the speed of a website ;
  • secure it;
  • limit spam ;
  • but also… set up redirects.

This file is very sensitive and requires a lot of caution, otherwise…

The slightest small error can make your site inaccessible. If you are not confident, I advise you to use the Redirection plugin.

If you still want to go on an adventure, remember to save your original .htaccess file before doing anything.

Then connect to your website using your favorite FTP client (Filezilla, Cyberduck or Transmit, for example).

Locate your .htaccess file and open it with your text editor. Finish by adding the line of code of your choice, preferably at the bottom of your file.

Let's go through some of them together.

# Redirect from URL to another
Redirect 301 /old-page/ https://mywebsite.com/new-page
# Redirect a category 
Redirect 301 /category/technology/ https://www.mywebsite.com/category/tech/
# Redirect a website from non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301]

The classic method for creating redirects in WordPress is especially useful for a complete change of the site URLs (for example when changing the domain name or moving from an HTTP address to an HTTPS address).

# Redirect a domain
RewriteEngine on
RewriteRule ^(.*)$ https://new-domain.com/$1 [R=301,L]

Of course, both the old and the new domain name must point to your website at the hosting provider.

To verify this, go to your old website. If it automatically redirects to the new one, it's all good. Otherwise, go to your host's interface and modify the score.

# Redirect to HTTPS 
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

If you want to go further with .htaccess, I suggest you read this article by Alex, entitled “The ultimate guide to the WordPress .htaccess file”.

For developers only: WordPress redirects in PHP

Redirects with wp_redirect()

There is yet another way to redirect URLs with WordPress: by using the wp_redirect() function with the template_redirect hook. Here is the function's syntax:

function my_awesome_redirection(){

	wp_redirect( $url, $status );
	exit;

}
add_action('template_redirect','my_awesome_redirection');

$url is the new address to which to redirect a URL and $status is the type of redirection performed.

However, this function should not be used lightly! In the case of our example above, all pages of the site would be redirected to the new URL.

It's best to use this function to create…

Conditional redirects

They allow you to create a redirect if the Internet user is in a specific situation (whether he is connected to the website or not, if he arrives on a 404 page, another page of the website, etc.).

For example, let's look at page 404. We were saying earlier that your users don't like to land on it. We could create a redirect so that they are automatically redirected to another page, the homepage for instance.

We would then write, in the functions.php file:

/* Redirect all 404 to homepage*/

function wpm_404_redirect(){
    if( is_404()){
        wp_redirect( home_url() );
        exit;
    }
}
add_action( 'template_redirect', 'wpm_404_redirect' );

Another case, which creates more complex conditions. Let's say you have a Resources page on your website, that's only accessible to your subscribers.

When your unregistered visitors arrive on this page, they may be redirected to a page that invites them to register. And, after validating their registration, they could be redirected to the Resources page.

Then we'd have:

function wpm_resources_redirect(){
    if( is_page( 'resources' ) && ! is_user_logged_in() ){
        wp_redirect( home_url( '/registration/' ) );
        exit;
    }
}
add_action( 'template_redirect', 'wpm_resources_redirect' );

Let's open Google Translation: this function, into common language, means:

If the Internet user is on the Resources page and is not connected to the site, then he is redirected to the https://monsite.com/inscription/ page.

Then, we would use the registration redirect hook to bring him back to the Resources page after his registration.

function wpm_redirect_registration_resources( $registration_redirect ) {
	return home_url( '/registration/' );
}
add_filter( 'registration_redirect', 'wpm_redirect_registration_resources' );

That's it! See how far you can go far with redirects?

To learn more about the features to use, take a look at the WordPress developer documentation.

What about you?

You have just finished reading this article detailing our methods to set up redirects in WordPress.

As you have understood, there are many ways to achieve this. The easiest and quickest way to do this is to install the Redirection plugin, which is essential for any type of WordPress site.

Whether you are blogging, publishing your work on a portfolio, selling products or whatever, there is always a time when you will redesign the architecture and content of your website.

Whether these changes are big or small, it's important to make sure that Google falls on 404 pages as little as possible, and that they don't impact your visitors' navigation.

The Redirection plugin is very easy to use: try it, you'll love it. And you no longer have any excuse not to hunt for your 404 pages!

But maybe you already manage your redirects? If so, which method do you prefer? Tell us all about it.

And please share this article on social networks, if you think it would be useful to others! Together, we'll end 404s 😉