One of the most annoying errors on websites are broken links, going nowhere or showing completely different content than expected. Neither Internet users nor Googlebot like them. The way to avoid incorrect links in Drupal is to use the Redirect module.
The redirects created by this module are extremely useful when there are changes being introduced to the URL structure of the website - both global ones and those caused by simple editing of individual content. Redirects ensure that the user will always find what they are looking for, even if the URL is no longer valid.
Dates
The module was first made available in 2006 by Moshe Weitzman in a very simplified form. The first alpha version was released in 2010, and a stable version was released for Drupal 8 in 2017. For several years Redirect has been developing much more dynamically, with the support of the Drupal.org community.
Popularity
Even the lack of a stable release for Drupal 7 didn’t discourage web developers from using the Redirect module. According to the official statistics, it’s currently used on over 260 thousand websites, 45% of which use Drupal 8.
Module's creators
The module is being looked after by three programmers:
- Sascha Grossenbacher - Lead Developer at MD Systems,
- Dave Reid - Senior Developer at the Lullabot,
- Oleksandr Dekhteruk - Senior Software Engineer at Epam.
The last few releases had a lot of support from other Drupal.org users. So far, over 110 people have contributed to the creation of the module.
Purpose of the module
The Redirect module is used to create URL redirects within Drupal. Thanks to it, you can redirect the user from one address within the website to another. Note that there is a big difference between a redirect and an alias.
- An alias is the "user-friendly" URL of the content that you see in the address bar of a web browser. If you won't define an alias for a given subpage, it will have a default address like “/node/123” or “/ taxonomy/term/456”.
- A redirect is a rule that's triggered when you reach a given URL address. The rule specifies the target address the user will be redirected to, the language for the redirect, and the type of HTTP code returned. The target webpage will only appear in the browser's address bar when there are no more redirects to be carried out.
The URL redirects are primarily used to improve the linking structure within a webpage. Redirect, however, has a few more interesting applications resulting from the fact that in the version for Drupal 8 it received some of the functionalities of the Global Redirect module. I’ll mention them later in this article.
Unboxing
You can download the Redirect module from the Drupal.org website. The recommended installation method is to use the Composer command:
composer require drupal/redirect
You can find the module settings panel by going to Configuration → Search and Metadata → URL Redirects.
Module's use
The main element of the module's configuration is the list of redirects:
To add a new redirect, select "+ Add redirect" and then fill in the necessary information, i.e. source address, target address, HTTP code and language.
It's good to explore the seven available HTTP codes. This is because they greatly affect the SEO. You will definitely need:
- 301 Moved Permanently - the requested page has permanently changed its address. This is an important signal for search engines, which remove the previous page address from their resources and replace it with the new one.
- 302 Found - the requested page is temporarily available under a different address. This code is useful in a situation where you test changing the linking structure on a webpage, but you want the change to be noted by search engine robots.
Other available codes are:
- 300 Multiple Choices - there is more than one way to process the given query (rarely used, e.g. for processing different video formats).
- 303 See Other - the response to the request is under a different address (used e.g. for POST requests).
- 304 Not Modified - information for the browser that the webpage hasn’t changed since the last visit and it can be loaded from the cache.
- 305 Use Proxy - the requested webpage is only accessible via proxy.
- 307 Temporary Redirect - same as 302, but with the assumption that the HTTP method can't be changed.
Additional options
When you've mastered creating URL redirects, check the "Settings" tab. You'll find there two important options that will help you achieve better results with Google.
- Automatically create redirects when URL aliases are changed - be sure to select this setting. It'll help you keep the webpage clean in the case of changes to internal linking. When you modify any alias on a webpage, a redirect from the old address to the new one will be automatically created.
- Enforce clean and canonical URLs - this is the option that convinces many developers to choose the Redirect module. It redirects the user to the canonical address of the given webpage, so that they won't stay under a working address like "/node/123". This setting was available in Drupal 7 through the Global Redirect module.
Hooks and integrations
The module provides the following hooks:
- hook_redirect_load() - launched when loading an array with redirects, allows you to add new items to it,
- hook_redirect_load_by_source_alter() - used to modify redirections for a given source address,
- hook_redirect_prepare() - changes the form of a given redirection in the module's administration panel,
- hook_redirect_alter() - allows you to change the settings of a redirect just before it is triggered.
In addition, the Redirect works with the Pathauto module. It's a very harmonious tandem for managing creating redirects when changing page aliases.
Summary
The ease of creating and handling redirects is an important feature of a professional CMS. The Redirect module does this very well. It’s a stable solution that has been developed over the years. I recommend using it in any Drupal development project.