How to make a Wordpress redirection?

Discover the methods to operate an effective redirection. Whatever redirection you need, we have the solutions to help you.
Table of contents

Redirects are a crucial part of a website 's daily routine to ensure that users and search engines are accessing the correct URLs on your Wordpress site. Any good SEO knows that they can be used to redirect users to pages that have been moved, deleted or renamed, as well as to correct typos and duplicate URLs. Redirects can also be used to create aliases for URLs or to temporarily redirect to specific events or promotions.

Discover all the redirection methods used by Wordpress agenciesagencies, each with its own advantages and disadvantages. In this article, we will explore the different methods available, including the use of plugins, the modification of .htaccess files, the use of the redirect_canonical() function of Wordpress, the use of PHP code including the wp_redirect() function or the wp_safe_redirect() function, and the use of the Guzzle library. We will also give you the pros and cons to help you choose the best option for your site.

Using a redirection plugin

A pink notebook on a yellow background with a sign that reads "Wordpress Plugin".
Using a plugin for your redirects is the most common solution and also the easiest to manage over time

There are many redirection plugins available on the Wp repository, such as Yoast, Redirection and Simple Redirects to manage redirects on Wordpress; but for an efficient and simple use, Rank Math Pro is the ideal solution. At Ingenius, we have chosen Rank Math Pro because of its proven reliability and its continuous evolution to meet our SEO needs.

Benefits

  • Ease of use thanks to a simple user interface
  • Ability to configure mass redirects
  • Ability to track and automatically redirect 404 errors

Disadvantages

  • Can slow down loading times if the tool is not properly configured or uses too many resources
  • Requires regular updates to fix bugs and security vulnerabilities

Explanations of the method 

The plugins in question are designed to simplify the creation of redirects using a user-friendly interface. This means that you don't need any knowledge of web development to use this method.

The Rank Math plugin of the Ingenius website opened on the redirection management section
Rank Math includes a clear management of your redirects that allows you to add and modify them as you wish

To use an extension such as Rank Math Pro, you must first install and activate the file you have uploaded to your client area. Once activated, you can access the tool's redirection settings from the Wp settings menu. In the settings, you can create new rules by selecting the origin and destination page. You can also track 404 errors and create bulk redirects from this section.

Examples of use

Example 1: You have moved a page from one location to another on your site. To prevent users and search engines from getting a 404 error, you can use a redirect tool to redirect all requests for the original URL to the new URL.

Example 2: You have created a temporary promotion for your site and want to redirect users to a specific URL when they click on a promotional link. You can use a plugin like Rank Math to do the redirection.

Modification of .htaccess files 

.htaccess files are Apache configuration files that can be used to perform redirects. To use this method, you must have access to your site's .htaccess and have some knowledge of URL rewriting and the use of Apache directives.

The htaccess of a Wordpress used to manage redirects
Editing htaccess should not be taken lightly, only touch it if you know what you are doing!

Benefits 

  • Ability to configure advanced redirects
  • Higher performance than plugins 

Disadvantages 

  • Requires knowledge of URL rewriting and use of Apache directives
  • Can cause security problems if redirects are misconfigured

Explanations of the method 

The .htaccess is used to configure Apache settings for a given site. Using appropriate Apache directives, you can use.htaccess to create redirects for your site's URLs. This method can be faster and more efficient than using plugins, as the redirects are handled directly by the server.

For this to work, you need to access your site's .htaccess via an FTP client like FileZilla or by using your hosting's file editor. Once you have access to the .htaccess file, you can use Apache directives to create redirects. For example, to redirect one URL to another, you can use this directive: 

"Redirect 301 /oldpage.html /newpage.html"

It is important to check the syntax and test the redirects before saving and uploading the file, as a syntax error can cause problems for your site. It is also important to save a copy of the file before making any changes, in case you need to undo the changes.

Examples of use 

Example 1: You have renamed a page on your site and want to redirect all requests for the old URL to the new one. You can use this directive in the .htaccess to redirect users to the new URL.

"Redirect 301 /ancienne-page.html /nouvelle-page.html"

Example 2: You want to redirect all requests for a sub-folder to another sub-folder. You can use this directive in the .htaccess to redirect all requests for the sub-folder to the new sub-folder.

"RedirectMatch 301 ^/ancien-sous-dossier/(.*)$ /nouveau-sous-dossier/$1"

Use of PHP code 

It is possible to use PHP code to perform redirects on a Wp site. This requires some PHP programming knowledge and can be more complex than using an extension or .htaccess modifications. However, it allows for more flexibility and more advanced redirects.

Representation of a keyboard with a big blue key that means "PHP".
PHP allows you to apply your redirections if you have knowledge of code

Benefits 

  • Ability to configure advanced redirects
  • Higher performance than plugins 

Disadvantages 

  • Requires knowledge of web development to write and implement the code
  • Can cause security problems if redirects are misconfigured

Explanations of the method

Using PHP code allows for more flexibility and more advanced redirects. This requires knowledge of PHP and can be more complex than using a plugin or .htaccess modifications. This type of redirection is usually used for specific cases and requires an understanding of the URL structure and programming logic.

To use PHP code, you need to add custom code to a PHP file in your theme or to a custom plugin. There are several commonly used PHP functions to perform redirects, such as header() and wp_redirect()

Examples of use

 Example 1: You want to redirect users to a specific page based on their user role. You can use the following PHP code to check the current user role and redirect to the appropriate URL:

if(current_user_can('subscriber')) {
   wp_redirect(home_url('/page-pour-subscribers/'));
   exit;
} elseif(current_user_can('editor')) {
   wp_redirect(home_url('/page-pour-editors/'));
   exit;
}

Example 2: You want to redirect users to a specific page after submitting a form. You can use the 'wp_redirect' function to redirect users to the thank you page after submitting the form:

wp_redirect(home_url('/thank-you-page/'));
exit;

Using the wp_redirect() function

The wp_redirect() function is used to perform redirects to another page or site. This function is used to redirect users to another URL after a specific action, such as submitting a form or logging into an account.

Benefits 

  • Ability to redirect users to another page easily
  • Higher performance than plugins 

Disadvantages 

  • Requires knowledge of web development to customize the function
  • Can cause security problems if redirects are misconfigured

Explanations of the method 

The wp_redirect() function is a function that is typically used to redirect users after a specific action, such as submitting a form or logging into an account. It is important to note that this function must be used before any content is displayed, otherwise it may cause errors.

To use the wp_redirect() function, you need to add code to a PHP file in your theme or to a code injection tool. The function takes as argument the URL of the page or site you want to redirect users to, as well as an optional HTTP status code (by default, it is a 301 status code). 

Examples of use

Example 1: You want to redirect users to a thank you page after submitting a form. You can use the wp_redirect() function to redirect users to the thank you page after submitting the form:

wp_redirect(home_url('/thank-you-page/'));
exit;

Example 2: You want to redirect users to a specific page after they log out of their account. You can use the wp_redirect() function to redirect users to the logout page after they log out of their account:

wp_redirect(home_url('/logout-page/'));
exit;

Using the wp_safe_redirect() function

The wp_safe_redirect() function is used to securely redirect to another page or site. This function is similar to the wp_redirect() function, but checks that the redirect URL is on the same domain as the current site, thus preventing malicious redirects.

Benefits 

  • Verifies that the redirection is to a trusted site before redirecting the user
  • Higher performance than plugins 

Disadvantages 

  • Requires knowledge of web development to customize the function
  • Can cause security problems if redirects are misconfigured

Explanations of the method

The wp_safe_redirect() function is a Wordpress function similar to the wp_redirect() function with a special feature that prevents malicious redirects. It is important to note that, as with the previous function, this one must also be used before any content is displayed, otherwise it may cause errors.

As with the previous function, you need to add code to a PHP file in your theme or in a code injection extension. The function takes as argument the URL of the page or site you want to redirect users to, as well as an optional HTTP status code. The wp_safe_redirect() function also checks that the redirect URL is on the same domain as the current site, which prevents malicious redirects.

Examples of use

Example 1: You want to redirect users to a thank you page after submitting a form. You can use the wp_safe_redirect() function to redirect users to the thank you page after submitting the form:

wp_safe_redirect(home_url('/thank-you-page/'));
exit;

Example 2: You want to redirect users to a specific page after they log out of their account. You can use the wp_safe_redirect() function to redirect users to the logout page after they log out of their account:

wp_safe_redirect(home_url('/logout-page/'));
exit;

Using the redirect_canonical() function 

The redirect_canonical() function of Wordpress is used to manage redirections for duplicate URLs. It allows to redirect users to the canonical version of a URL, i.e. the one that has been defined as the official version of a page

// Redirige l'utilisateur vers l'URL canonique
add_action( 'template_redirect', 'redirect_to_canonical' );
function redirect_to_canonical() {
  // Vérifie si l'URL actuelle est canonique
  if ( !is_singular() && !is_home() && !is_archive() && !is_category() && !is_tag() && !is_author() && !is_search() && !is_404() ) {
    // Renvoie l'URL canonique
    redirect_canonical();
  }
}

Benefits 

  • Automatically redirects duplicate URLs to the canonical URL
  • Higher performance than plugins 

Disadvantages 

  • Requires knowledge of web development to customize the function
  • Can cause security problems if redirects are misconfigured
  • Can cause harm to the customer experience (in the context of an e-commerce)

Explanations of the method 

The redirect_canonical() function of Wordpress is designed to manage redirections for duplicate URLs. It allows to redirect users to the canonical version of a URL. This function is used automatically by Wp, but can be customized to meet the specific needs of your site.

To use the redirect_canonical() function for your redirects, you need to add custom code to your functions.php file or to a custom plugin. You can use the redirect_canonical() function to redirect URLs with parameters, URLs with subdirectories, and URLs with ty pos using appropriate arguments. 

It is important to note that the redirect_canonical() function does not handle redirection for pages that have been moved, deleted or renamed. To handle these types of redirects, you will need to use another method such as a plugin or .htaccess changes.

Examples of use 

Example 1: You want to redirect users to the canonical version of a URL even if it contains unnecessary parameters. You can use the redirect_canonical() function with the "strip_qs" argument to remove unnecessary parameters from the URL before redirecting to the canonical version.

Example 2: You want to redirect users to the canonical version of a URL even if they used a typo in the NDD. You can use the redirect_canonical() function with the "trailingslash" argument to handle typos in the domain name and redirect to the canonical version of the URL.

Use of the Guzzle library method

The Guzzle library is an HTTP request management tool that can be used to perform redirects on a Wordpress site. This method requires advanced PHP programming knowledge and can be more complex than using a redirection plugin or touching the .htaccess. However, it allows for more flexibility and more advanced redirects.

Screenshot of the Github of Guzzle on the controls section
Guzzle is a much less popular option than anything else but still performs well

Benefits 

  • Ability to configure advanced redirects
  • Higher performance than plugins

Disadvantages 

  • Requires knowledge of web development and use of the Guzzle library to set up redirects
  • Can cause security problems if redirects are misconfigured

Explanations of the method

The use of the Guzzle library to perform redirects allows for greater flexibility and more advanced redirects. This method is used for specific cases and requires knowledge of URL structure and an understanding of programming logic.

To use the Guzzle library, you must first install the library using Composer or by downloading the necessary files. Then you can use the library functions to send an HTTP request and manage redirects. 

Examples of use 

Example 1: You want to redirect users to a thank you page after submitting a form. You can use the Guzzle library to send an HTTP request to the thank you page and manage the redirection:

$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'https://example.com/thank-you-page/');
$statusCode = $response->getStatusCode();
if ($statusCode === 301) {
    $location = $response->getHeader('location')[0];
    wp_safe_redirect($location);
    exit;
}

Example 2: You want to redirect users to a specific page after logging out their account. You can use the Guzzle library to send an HTTP request to the logout page and handle the redirection:

$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'https://example.com/logout-page/');
$statusCode = $response->getStatusCode();
if ($statusCode === 301) {
    $location = $response->getHeader('location')[0];
    wp_safe_redirect($location);
    exit;
}

In conclusion, how to operate a redirection on a Wordpress?

An illustration that represents a 301 redirect between 2 Wordpress
Redirects on Wordpress are common, you use the method that meets your expectations

Redirects are an important element for a Wordpress site, as they help maintain a smooth user experience and protect SEO on Woocommerce. Take the trouble to understand the mechanics to avoid unpleasant surprises and wondering "Why is my site not showing up on Google?". There are several methods to perform redirects on Wordpress, including using plugins, modifying .htaccess, using PHP code and using functions such as redirect_canonical() and wp_redirect(), using specific plugins to redirect 404 pages, using the wp_safe_redirect() function and using the Guzzle library

Each of these methods has its own advantages and disadvantages, and it is important to understand these differences to choose the method that best suits your needs. It is also important to note that it is important to test and verify redirects to ensure that they work properly and do not cause infinite loops or other problems. By following the instructions in this article, you should be able to understand the different redirection methods available and use them effectively to improve the user experience and protect the SEO of your Wordpress site.

And if despite all this information, you remain drowned in a sea of chaotic redirects, ournatural referencing subscription is made for you. Indeed, our team ofWordpress experts will take charge of your entire strategy, including all the redirects to be set up!

What about you? Do you have any other tips for performing a redirect on Wordpress? Write us your strategies in the comments, we would be happy to discuss them with you!

The Wordpress agency that designs and builds high quality and sustainable websites.