URL redirects come handy when the website is under construction or during domain migration.
In Drupal, URL redirects simply redirect visitors to the proper page, even when the original website has errors.
Also, websites with too much content may often need merging or deleting pages. Therefore, to maintain the traffic from those merged pages, URL redirects is vital.
At Bobcares, we often get requests from our customers set up Drupal URL redirect as part of our Server Management Services.
Today, we’ll see how our Support Engineers configure URL redirect in Drupal and fix the related errors.
How we redirect URL in Drupal
The Redirect module is a third party module that allows to create and manage redirects & to maintain search engine ranking.
By default, 301 code is useful for permanent redirects.
For SEO purpose and to tell Google to index the new URL we use 302 code.
Let’s see how our Support Engineers do URL redirection in Drupal 8.
Using the Redirect module
1. Initially, we install the Redirect module & enable it using this command:
drush dl redirect
2. Next, we go to Configuration setting and hit on URL redirects > Add redirect.
3. Under Add URL redirect, we give the old URL in Path field & new address in To field.
4. From the Redirect status drop-down box, we select 301 Moved Permanently option.
5. At last, we click the Save button.
You can track the number of times a redirect is used by checking the statistics in Count column from the URL redirects list.
Using .htaccess method
An alternative technique to add the URL redirect is by using the .htaccess file. It works with mod_rewrite rule & regular expressions for redirecting old URL.
For this, we open the root directory where Drupal’s folders are installed. Then, we locate the .htaccess file & add the following code to redirect.
RewriteEngine On RewriteRule ^old_URL.html$ http://example.com/x.php [R=301,NC]
This redirects any “.html” page to the complete URL ending with “.php”.
Now, you will be redirected back to the Redirect page in which you can manage existing redirects and create new ones.
Related issues and fixes
From our experience in managing server, we’ve seen that customers facing problems related to URL redirection in Drupal.
Let’s take a look at the top problems and how we fix them.
1. Redirection not working in the root folder
Recently, one of our customers contacted us with trouble in the URL redirection. His website was under maintenance and renamed index.php to index_drupal.php as the root script. This resulted in the failure of redirection.
Generally, for redirection to work properly we should use index.php as root script by default.
In this phenomena, the user renamed it as index_drupal.php, that caused the redirection to not work correctly.
To get around this problem, our Support Engineers set the redirection rule in the .htaccess file under the root directory.
That fixed the problem and the user could redirect URL properly.
2. Invalid URL
Similarly, another customer reported a problem with 301 redirects. He entered the path starting with “/” that resulted in an error like invalid URL.
In Drupal 8, paths follow the HTTP specification, which means they do have a leading slash. Therefore, users enter the full path with a leading backslash by mistake.
Furthermore, the path field in Add URL redirect setting doesn’t require a leading slash. So, entering the correct path without a backslash solved the problem.
Now, the user could use 301 code to redirect the URL.
[Having trouble with Drupal URL redirection? We’ll fix it for you.]
In short, during domain migration or content audit, URL redirects is a good technique to redirect visitors to access the correct page without any failure. Today, we saw how our Support Engineers redirect URL in Drupal using alternative methods & fixed the related issues.