Automatically Generate URL Aliases using Pathauto in Drupal 8

Pathauto is a module which lets you automate the generation of URL aliases in Drupal. Instead of the URL being “/node/123”, you can have “/blog/article/why-use-drupal”.

The module allows you to define custom patterns which are generated when an entity is created.

URL aliases or URL slugs, help with search engine optimization and they’re more user-friendly.

Drupal core has supported URL aliases for a long time, but they weren’t automatically generated. Pathauto helps with automating the process.

In this tutorial, you’ll learn how to create aliases and patterns, and how to bulk generate paths.

Getting Started

Before we begin, go download and install the following modules:

  1. Pathauto
  2. Token
  3. Ctools

Using Drush:

$ drush dl pathauto token ctools
$ drush en pathauto

Or, using Composer:

$ composer require drupal/pathauto

Manually Create URL Aliases

Pathauto is not required to create aliases. Drupal core uses a module called Path to create them, and it depends on this module. Pathauto simply helps you automate the creation process.

URL aliases can be created in two ways: from the content edit form and the “URL Aliases” page.

To create an alias from the form, click on the “URL path settings” field-set on the right of the form. Then enter the path into “URL alias”.

Another way, go to Configuration, “URL Aliases” and click on “Add alias”.

Create Pathauto Patterns

Let’s first look at how to setup Pathauto patterns. A pattern lets you define what the structure of the URL alias should be. For example, we’ll add “article/[node:title]” for the Article content type.

The module will convert “article/[node:title]” to “article/node-title”. [node:title] will be replaced by the article title.

1. Go to Configuration, “URL aliases” and click on the Patterns tab.

2. Click on “Add Pathauto pattern”.

3. Select Content from “Pattern type” and enter “article/[node:title]” into “Path pattern”.

If you want to see all available tokens, click on “Browse available tokens”.

4. Check Article from “Content type”.

This means that this pattern will only be applied to Article content types.

5. And finally, add Article into Label.

Then click on Save.

Generating an Alias

If you go to the “URL path settings” on a content type, you’ll notice that it looks different once a pattern has been enabled. Now you get a new checkbox “Generate automatic URL alias”.

If this stays checked, then an alias will be generated. If you want to override the generated one, then uncheck it and add your custom alias into the “URL alias” field.

Pathauto Settings

The module settings can be configured by clicking on the Settings tab from the “URL aliases” page.

You can configure a lot on this page, but the few important ones are:

Enable entity types

This lets you turn on Pathauto support for custom entities.

Update action

This lets you define what the module should do when an entity is updated.

Strings to Remove

This lets you define which words will be stripped from the alias.

Punctuation

This allows you to control how special characters are handled.

Now just a friendly warning. Do NOT play around with these settings on a live site. The last thing you want to do is break the URLs on a site that’s already in production. Backup the database before you make any changes.

Bulk Generate Aliases

If you already have a ton of content and want to generate aliases or you want to regenerate them, you can do this by clicking on the “Bulk generate” tab.

First, select which entity type you want to bulk generate. Then select which aliases you want to be generated.

Before running any bulk generation make sure you backup your database.

Delete Aliases

You can batch delete aliases from the “Delete aliases” tab. You can choose which entity types you want to be deleted, or delete all aliases.

But take note of the “Delete options”, make sure you check “Only delete automatically generated aliases”.

This won’t delete aliases which are manually created.

Menu Structure as Path

The challenge in creating a good pattern is trying to figure out which token to use.

Just click on “Browse available tokens.” and look at all the available options. It can be overwhelming to try and figure out what token does what.

One common pattern which I’ve used a few time is to have a path use the parent menu path.

Take for example the following structure:

- Drupal (/drupal)
-- Site Building (/drupal/site-building)
--- Using Views (/drupal/site-building/using-views)

Just imagine the above example is part of the main navigation. “Drupal”, is the first level, “Site Building” is the second and “Using Views” is the third.

Notice how the path for “Using Views” has its parent path, “/drupal/site-building/using-views”. To achieve this type of path just use “[node:menu-link:parent:url:path]” to get the parent.

The full pattern with the title will be: “[node:menu-link:parent:url:path]/[node:title]”.

If you know of any useful tokens, let us know by leaving a comment.

Summary

Pathauto is an essential module which I’ve installed on every Drupal site I’ve worked on. The importance of URL aliases isn’t obvious at first. But if you spend a bit of time coming up with a good set of patterns, it’ll help your site rank well in search engines.

Ivan Zugec

About Ivan Zugec

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 10 years and has successfully completed several large Drupal projects in Australia.

16 thoughts on “Automatically Generate URL Aliases using Pathauto in Drupal 8”

  1. Devaraj johnson B

    Menu Structure as Path is great and it will be really useful and its new great work you made learning Drupal easy Ivan 🙂

    1. Hi Keha,

      When you say ‘tags’ do you mean Taxonomy?

      Defining patterns for taxonomy entities is the same as defining it for Content entities.

      Cheers,
      Ivan

  2. Hi Ivan. I agree that this module is essential to every site. This is a great tutorial on how to get Pathauto up and running. Though it’s probably worth mentioning the Redirect module for people who are new to this so as to avoid being able to access the page from both the alias and the node/xx path, as this can actually harm your SEO.

    So I’ve got an interesting use case that I’m not sure can be solved using Pathauto, but thought you’d be the best person to ask.

    1. Hi Matt,

      You’re correct. In Drupal 8, the Redirect module can handle 301s from node/xxx to the URL alias. In Drupal 7, “Global Redirect” would handle this. Luckily the functionality has been merged into Redirect for D8.

      Cheers,
      Ivan

  3. Essentially, I’ve got Pathauto all set up and running nicely. However, using the standard convert title method I end up with URLs that are fairly long e.g. https://digitalbeanstalk.com.au/blog/6-digital-marketing-solutions-every-restaurant-should-try . Now this is not so much of an issue when sharing in Facebook or LinkedIn, however, it’s far too long for Twitter, Instagram or even in emails. I’d rather not use a generic URL shortener, so I’ve registered a short URL which redirects to my canonical URL, including the path. So now the above URL becomes dbean.cc/dms . To get this working I currently need to manually input the redirect. Not a huge problem for my own site, but not a solution that would work for a client website.

    Ideally what I would like is a simple text field when editing the page, let us call it short_url. Metatag can then use that to generate the short url to put into the metadata, and Pathauto can use it to create a redirect to the page alias. As mentioned before there can only be one path the page is actually accessed from to avoid any SEO penalties. Using the combination of Pathauto and Redirect do you know of any workflow that can produce this result?

    Thanks in advance
    Matt

    1. Hi Matt,

      I do understand what you’re asking for and you’re on the right path.

      From a branding perspective it’s much better to use your domain name than say bit.ly.

      There is a Drupal 7 module which does exactly what you’re looking for, however, there’s no Drupal 8 port. 🙁

      https://www.drupal.org/project/shurly

      You can build something custom using aliases and Redirect.

      – Create a field short_url (as you’ve mentioned)
      – Programmatically create a redirect from the value in short_url
      – You’ll need to validate and make sure the value in short_url is Unique, look at Field validation, https://www.drupal.org/project/field_validation
      – Another thing to think about is, can an editor change the short_url value after the form has been submitted

      Hope this helps.

      Cheers,
      Ivan

      1. Hi Ivan,

        I agree with the branding viewpoint. Where I can I use the canonical domain name. Just some situations require a short URL. That’s also why I’ve registered my own rather than using something like bit.ly.

        Shame that there’s no port to Drupal 8 for the shurly module. I was hoping to avoid some custom code but did suspect that might the case. Some good points to think about there, especially making sure it is unique. I can see a case where an editor might want to change the short_url value, I think the site would just need to make sure the existing redirect remains and add the new one.

        Thanks for your advice.

        Matt

  4. Hi!!
    First thanks for you article.
    I have a question, if I like that all routes that begining from a url (for instance “/base-url/random1/random2/?param1=value1”), go to the same url (“/base-url”), How can I do it?

Leave a Comment

You have to agree to the comment policy.