Manage URL Redirects using Redirect module in Drupal 8

The ability to create and maintain redirects on a website is vital for long-term success.

Once your site has a lot of content, you may need to do a content audit. This will require merging or deleting pages which are no longer important. To maintain the traffic from these deleted or merged pages, you’ll need to create URL redirects. Now I understand this isn’t the most exciting part of site building but it’s important to get it right.

The module which will handle all of this is perfectly named; Redirect.

The Redirect module lets you create and manage redirects using a simple user interface. Just define a source and destination and you’re good to go. You can also track 404 errors, using a sub-module, so if you have a page indexed in Google with a broken path then it’ll be logged and a redirect can be easily created.

Learn how to automate the generation of URL aliases using Pathauto in Drupal 8.

Getting Started

Before we begin, make sure you download and install the Redirect module.

If you use Composer, run the following:

$ composer require drupal/redirect

Or Drush,

$ drush dl redirect

Create a Redirect

Once you have installed the module creating a redirect is very easy.

1. Go to Configuration and click on “URL redirects”.

2. Click on “Add redirect”, enter in a Path (old path) and then select a To (new path).

The To field is an autocomplete field which you can use to lookup content on the site. But you can also add an external URL into the field.

3. From the “Redirect status” drop-down box, you can select which status will be used. Most of the time it’ll be a 301 or 302. Once you’re finished click on Save.

4. You’ll be redirected back to the Redirect page where you can manage existing redirects and create new ones.

Track Broken Paths

The Redirect module ships with a handy sub-module called Redirect 404.

The sub-module logs all the 404s and displays them all with a count in the “Fix 404 pages” page in “URL redirects”. When dealing with redirects, half the battle is figuring out which paths need to be redirected. This sub-module will log all 404s and add a button called “Add redirect” which you can use to create the redirect.

This sub-module could cause performance issues if your site gets a lot of traffic so test accordingly.

Domain Level Redirects

Another sub-module, which Redirect comes with is called “Redirect Domain”, it allows you to create domain level redirects.

A good use of this is when you want to redirect a whole domain, i.e., old-domain.com to new-domain.com. You can create a rule which will redirect anything from old-domain.com/* to new-domain.com, without manually creating redirects.

Import Redirects

Creating redirects manually won’t cut it if you need to add more than 20. If you have a spreadsheet full of redirects then it’s best to look at using Path redirect import.

The module lets you import redirects using a CSV file. All you need to do is prepare everything in a spreadsheet then once you’re ready, export the sheet as a CSV and import it into Drupal using the module.

The format of the CSV is pretty simple:

From,To,Redirect,Language
hello-world,node/1,301,en

From and To are the only required columns. Redirect and Language are optional.

Run CSV Import

Once you’ve installed Path redirect import, go to Configuration, “URL Redirects” and click on the Import tab.

Select a file using the CSV File upload field and click on Import. Once the import is complete you’ll see a message telling you which redirects imported and which didn’t.

The reason the redirects in the above image were bypassed (not imported) is because the page did not exist. When preparing your CSV file make sure the paths and nodes actually exist. For example, if you’re creating a redirect to node/123 and that page doesn’t exist then the module won’t import it in.

Update Existing Redirects

When importing, if you want to update existing redirects then all you need to do is check the “Override existing sources” checkbox when importing.

Global Redirect merged into Redirect Module

The functionality of the Global Redirect module has been merged into Redirect for Drupal 8.  This is great because it means there’s one less module to install.

The Global Redirect settings can be configured by going to the Settings page with “URL redirects”.

Summary

Redirect is one of the must-have modules which is installed on most Drupal sites. But you can use it for more than just managing historical links. Another good use-case is to create vanity URLs. For example, you could create a redirect path domain.com/d8, that’ll redirect to another page within your site. This is especially useful when adding URLs into a presentation, the shorter the URL the more memorable it is.

About The Author

21 thoughts on “Manage URL Redirects using Redirect module in Drupal 8”

  1. Great article. I wasn’t actually aware that there was a domain level module available. Up till I’ve been using cpanel to do this, which works for most cases but I can definitely see the benefit of being able to manage it directly through Drupal. One of the main uses I’ve found lately is what you mentioned in the summary with the vanity URLs. Though by combining the domain and path level redirects you can also create a short vanity URL e.g. yourfulldomainname.com.au/full-article-title becomes shorturl.cc/d8

    1. The only thing missing is an API to automatically generate the vanity URLs. So you could use something like Buffer or hootsuite and have the URL created when creating a status messages.

  2. I get this error message when I try to redirect to an external url.
    Manually entered paths should start with /, ? or #
    Any idea why this might be the case.

  3. I Evan, i am trying to redirect a content but it is not working in D8
    e.g.
    I have a node in unpublished state with a url /abc
    I am trying to redirect it to other url say /xyz … but it is not working ..
    It works if I use node/123 i.e. node id in Path and /xyz in To field.
    but now working with url alias to url alias.
    If I delete url alias /abc, then it is working ..
    what might be the issue… in D7 it was working fine

    1. Hi AV,

      Have you tried the Redirect module, what this tutorial covers? It does exactly what you are after unless I’m not understanding your question.

      Cheers,
      Ivan

  4. This looks like exactly what I need thank you.

    Will this module also be able to handle redirect wildcards?

    Suppose I have an old site with “example.com/old/path/[nid]” and I am moving this content to now be at
    “example.com/new-path/[nid]” and I want to provide redirects for any users that may have the old paths saved or bookmarked.

    Can this module provide this functionality or do you know of another module that can do this?
    Thank you in advance,

  5. Does anybody know of a method, plugin or other solution to set up wildcard redirects for Drupal 8? I know the Match Redirect module – but it hasn’t been ported to D8 yet. I have quite a few taxonomy terms that are for backend use only – I’ve used pathauto to alias them all to redirect/[term:name] … hoping I could then do a redirect to the home page, like so: /redirect/*

  6. Hey, I have more than 3000+ live URLs with a 404 page not found errors as product is no more available. I want to redirect them to the respective category page of the product.
    So I have created a file comprising of 3000+, 404 error pages and correct links of there pages which needs redirection.

    Can you please let me know what method can I use in Drupal to get this issue resolved?

    Looking forward to your support.

    1. Hi Jayesh,

      What exactly do you want to achieve? Import the redirects into Drupal?

      Just the read “Import Redirects” section of this tutorial.

      Regards,
      Ivan

  7. Hello, Ivan
    Thanks for writing a great article. I have a bit of a different issue I think.

    I’m migrating my site to a new theme. I’m using a theme switcher module so that I can build the new pages on production while still keeping the old theme. The new pages have the same URLs as the old pages, but with on small difference, for example:
    /products –> old theme URL
    /new/products –> new theme URL

    When the time comes to switch over to the new theme, I need to change all of the new URLs to remove the /new part. Essentially, I want to keep all of the old URLs but have the URLs point to the new nodes.
    What I was thinking was to archive all of the nodes that are using the current URLs, then go to each new node and remove the /new part of their URLs, but this will be very time consuming and error prone as there are hundreds of new pages.
    Alternatively, I was thinking to bulk archive all of the old nodes and in the .htaccess file rewrite the new URLs to be the old URLs like so:
    RewriteRule ^/new/products$ http://example.com/products [R=301,L]

    Does my plan sound ok? Would you recommend this or some other way? Do you think your method above would work for my scenario? Thank you for any advice you might have!

    1. Hi,

      Your requirements go way beyond what redirect can handle.

      In your situation I would have spun up another version of the site to test the different theme. Once you’re ready to go live then just move the new site over to production.

      Something like, new.example.com/products.

      Cheers,
      Ivan

  8. Thank you for this, Ivan.

    I was wondering if you have any suggestions on how I could do the following in D8/D9.
    I would like to have a generic URL called “/current-newsletter”.
    This should redirect to the latest Newsletter node that is published (or conditions based on a View filter criteria).
    In D7, I had a page view and a views PHP field that did the following:

    $alias = drupal_get_path_alias(‘node/’ . $row->nid);
    drupal_goto( $alias);

    However this is not possible now in D8/D9.
    Any pointers or suggestions would be much appreciated.
    Thank you, Ivan!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top