Broken links can be very problematic. How to deal with them in Drupal? It is best to use the Redirect module. In this article, you will learn what this tool is, how to make a redirect in Drupal and what similar modules can be helpful.
Table of Contents
Drupal Redirect Module
Subpages on the website can stop working for various reasons. Deleting them will not solve the problem completely, as users can still reach them from external links, for example. This is why 301 redirects are used. When Google or users come across an address, they will be redirected to a new address. 301 redirect does not negatively affect the Drupal SEO power transferred between the subpages.
In some cases, a redirect to another page is not necessary. Let’s assume the title of the blog article has changed from “Best Drupal 10 E-commerce Modules for 2022” to “Best Drupal 10 E-commerce Modules for 2023”. There has only been a small update of the text. In this case, you do not have to create a new subpage, but only update the URL in the redirects to take, for example, this path: www.xyz.com/best-drupal-10-e-commerce-modules-for-2023.
The idea sounds simple, but adding a 301 redirect in Drupal can become a complex task, especially if you own an extensive website. That is why the Drupal Redirect module was created. Here are some most important pieces of information on the tool:
- release date – alpha version in 2010, stable release for Drupal 8 in 2017, currently supports Drupal 10,
- popularity – the Redirect module is currently used on over 270,000 sites,
- redirect function – admins can easily configure and manage redirects from old to new URLs directly from the Drupal interface,
- automatic redirects function – if you change the website address, the module can automatically create a URL redirect in Drupal,
- automatic redirects function depending on the type of content – the redirect module allows you to configure redirects conditionally. For example, when someone finds old blog posts about cooking tips, they will be automatically redirected to the main article, which collects them all in one place,
- error 404 tracking – one of the submodules of the tool allows 404 errors to be tracked.
Getting Started with Drupal Redirects Module in Drupal
You can install the Redirect module manually or with Composer. It is currently recommended to install all contrib modules using this tool. When installing modules manually, you cannot be sure whether additional dependencies will be installed. Composer eliminates this problem.
How to install the Redirect module using Composer? It is very simple:
1. Go to the Redirect module page
All information about the module can be found on the official Drupal website
2. Copy the module installation command
A ready-made module installation command is available in the “Releases” section.
composer require 'drupal/redirect:^1.9'
3. Execute the command from the command line
When the command is executed, you should see a message that the module has been added to the composer.json file in your project.
4. Enable the module
Click the “Extend” tab. Find the “Redirect” module in the list. Check the box next to it. Then click “Install”.
5. Define permissions
In the admin panel, go to “People” and select “Permissions”. Find the “Redirect” module and assign the appropriate permissions (e.g. “administer redirects”).
How to Use Redirect Module in Drupal?
The Redirect extension has many functions. Let’s take a closer look at them:
Before discussing how to set a redirect using the Redirect module, let’s focus on the initial configuration. Find the most important options under “Configuration > Search and metadata > URL redirects > Settings”. Then follow these steps:
- tick the box next to "Automatically create redirects when URL aliases are changed”,
- tick the box next to "Retain query string through redirect”,
- in "Default redirect status” select"301 moved permanently”,
- tick the box next to "Enforce clean and canonical URLs”.
Creating a Redirect Manually
Although the module can do most of the work for you, it is worth knowing how to manually create a redirect to an external URL. To do this, go to “Configuration > Search and metadata > URL redirects”. Click the “Add redirect” button. Then:
- enter the address that doesn’t work in the “Path” section,
- enter the new redirect destination address in the “To” section,
- select “301 moved permanently” in the “Redirect status”.
Editing the redirect
It is very easy to change the redirect URL. To do this:
- go to “Configuration > Search and metadata> URL redirects”,
- find the redirect you want to alter,
- click the “Edit” option next to the redirect,
- make the changes and click “Save”.
How to Fix a Redirect Loop?
An incorrectly set redirect can lead to a so-called redirect loop. In such a situation, link A redirects to link B, which then redirects back to link A.
How does it happen? Although Drupal automatically creates a redirect every time you rename a page, it will not automatically remove these redirects if you decide to revert to the previous URL. However, if you edit an existing redirect, the system will warn you if the change will lead to a loop.
- Find the redirect loop – some search engines display a pop-up which says “This website has a redirect loop”.
- Check the redirects – go to “Configuration> Search and metadata > URL redirects”. Find links that directly or indirectly lead to one another.
- Make changes – change the selected redirects to point to the correct URL or remove the unnecessary ones.
- Clear cache files – sometimes even after fixing the problem the cache layers (e.g. from Drupal or browsers) can still handle a looped redirect. In this situation, clearing the cache might help.
Submodule Redirect 404
The submodule logs all errors 404 and displays them on the “Fix 404 pages” page under “URL redirects”. It also adds an “Add redirect” option to quickly create redirects within Drupal.
When this module is enabled, several additional options are made available in the general redirects settings tab. They allow the number of errors 404 stored in the database to be configured, specific addresses to be omitted from the stored 404 errors (e.g. bot-generated attack attempts on addresses such as popular locations of unsecured tools giving access to the database or popular login page addresses), and all stored errors 404 to be cleared.
Submodule Redirect Domain
This submodule allows automatic redirects to be made at the domain level. For example, it is possible to create a rule, thanks to which every time someone visits any subpage on old-domain.com, they will be redirected to new-domain.com. Doing so many redirects manually would be very time-consuming, so it is worth remembering about it.
Import Redirects Module
If you have a ready-made collection of a dozen redirects or more, it will take a long time to enter them manually. Instead, it is better to use another extension called Path Redirect Import.
The tool allows you to import redirects using a CSV file. All you need to do is prepare the relevant data in a spreadsheet (the required structure is described above the import form) and then use Path Redirect Import to enter them into Drupal.
The module also allows existing redirects to be exported to a CSV file. This is particularly useful when you make a lot of changes to redirects. You can export existing redirects, edit them in a spreadsheet and then import the file with the changes. The possibility of deleting all redirects present in the imported file is also a useful option. Instead of manually deleting each redirect you don’t need, you can simply export the entire list, remove the redirects you want to leave on the website and import the file by checking “Delete redirects defined in the spreadsheet”.
The Redirect module can be very useful If you don’t know how to add 301 redirects in Drupal, Remember to pre-configure it properly – this will automate your work with managing errors 404, for example. In addition, the Path Redirect Import module allows you to quickly import a redirect group from a CSV document.
Always be aware of redirect loops as well. Although the tools discussed in the article can help prevent them from occurring, be particularly careful when setting up the rules, using multiple modules or switching from HTTP to HTTPS. We hope that thanks to this article, you learned how to redirect a page in Drupal.