Generate Automatic URL Aliases in Drupal 8: The Pathauto Module

How to Generate Automatic URL Aliases in Drupal 8: The Pathauto Module

Don't you just hate the "/node/007" type of “Drupalish” URLs? They say nothing to Google's bots and they sure look as some discouraging  hard-nut-to-crack enigmas to your site visitors, too. That Drupal core supports URL aliases in core is no news for anyone, but you still might be having some unanswered questions on how to generate automatic URL aliases in Drupal 8, right? Automatically creating URL slugs is the norm nowadays! This is precisely why I've done my best to put together a step-by-step guide for you on how to leverage the Pathauto module's full functionality on your Drupal 8 site and therefore automatize the whole path aliases creation process:

That Pathauto should be an essential module in your “emergency toolkit” as a Drupal developer is no news for you, right? Just imagine how the sites that you work on would look like and what ranking performance they would “score”, on a long term (not to mention the impact on the overall user experience, as well), as some “clusters” of “/node/xxx” URLs. 

And since the Pathauto module comes to turn generating URL aliases into an AUTOMATIC process (since “manually” creating each and every path alias, for each and every type of content on a given Drupal site, is such a... “dusty” old practice), I'll show you how you can harness precisely its “automation” superpower!

1. First Things First: Install and Enable the Due Modules

And these are:

The “trio” you'll need downloaded and enabled on your Drupal 8 site before you can even consider switching your attention to creating aliases and defining Pathauto's custom patterns.

Are you a Drush “addict” or a Composer fan? For you get to choose from these 2 methods for installing these key 3 modules:

a. enter the following command if it's Composer that you prefer:

$ composer require drupal/pathauto

b. or enter the following commands in Drush:

$ drush dl pathauto token ctools

$ drush en pathauto

Next, just run a quick check-up (simulating that you add a new piece of content) just to make sure that everything's working smoothly: go to Content > Add content and just select a content type from those listed there!

2. Now It's Time to Create Your URL Aliases (“Manually” This Time)

You don't need to harness Pathauto's power. At least not at this stage, when you're just creating your URL aliases.

The Drupal core module Path can “handle the job all alone” (Pathauto will just come, subsequently, to automate the whole creation process, get it?).

So, back to our sheep: we're manually creating our URL aliases.

And how do we do that? Luckily we have (again) 2 options to choose from (don't you just love it how there are always at least 2 ways of getting things done in Drupal?):

a. you can create them from your “URL aliases” page: Configuration > “URL aliases” > “Add alias”

How to Generate Automatic URL Aliases in Drupal 8: create aliases from your "URL aliases" page

b. or directly from your content edit form: click on “URL path settings” (you'll find this field, as shown in the image below, at the edit form's right side) and simply enter your path into the “URL alias” field there

How to Generate Automatic URL Aliases in Drupal 8: creating aliases from your content edit form

3. Define Your Custom Pathauto Patterns 

Since, as you well know it, aliases rely on pattern systems, it's you, the site's administrator/developer, that get to define these specific patterns and, thus, the aliases' structures.

Here's an example of an URL pattern: article/[node:title]!

What the Pathauto module will do is make it look less “generic” by naming it: it will replace the [node:title] part with the article's title itself.

And now, here's the step-by-step guide to follow for defining your own patterns as you generate automatic URL aliases in Drupal 8:

Step 1: Configuration “URL aliases” > click the “Patterns” tab

Step 2: Just give the “Add Pathauto pattern” blue tab a click

Step 3: Choose “Content” from the “Pattern type” drop-down menu that you have there and then just enter your “article/[node:title]” pattern into the “Path pattern” field on the same screen

 How to Generate Automatic URL Aliases in Drupal 8: adding your custom Pathauto pattern

Step 4: Select “Article” as your “Content type” (this way you're setting up that this specific custom pattern should get applied exclusively to the article content types on your Drupal 8 site)

Step 5: Type “Article” into the “Label” field

Step 6: Hit the “Save” button

Now that you've defined your custom pattern you'll notice a new checkbox “popping up” in your “URL path settings” field, on the right side of your content form: the “Generate automatic URL alias” one.
How to Generate Automatic URL Aliases in Drupal 8: check the "Generate automatic URL" box

  • once checked, it will automate aliases based on that specific pattern that you will have defined
  • once unchecked, you get to override the automatically generated one and to replace it manually with your own custom alias

Note: while you should check this particular box for new content, you might need to manually enable it for the already existing content 

4. Configure the Pathauto Module's Settings

Now in order to adapt Pathauto's functionality to your own Drupal 8 site's “needs” in terms of url aliases creation, you'll need to configure a setting or two (or three).

In this respect, here are 3 of the most common settings you might want to “tweak“:

  • Punctuation: you'll get to determine how special characters will be tackled
  • Enable entity types: you'll get to make use of Pathauto's support for custom entities
  • Strings to Remove: you're enabled to point out the words you want removed from the URL alias

Note: avoid, at all costs, tweaking these settings on your live Drupal 8 site! I know I shouldn't even be mentioning it, yet I am: backup your database before you go ahead and configure your Pathauto module's settings! This way you prevent the worse possible scenario to turn into reality: to end up with broken URLs on your live site!

5. Bulk Generate Automatic URL Aliases in Drupal 8

If it's a content-packed Drupal 8 website that you're dealing with, then you must be facing the challenge of bulk generating URL aliases.

Nothing easier! 

  1. Just click the “Bulk generate Tab”
  2. Choose the entity type that you need to bulk generate
  3. Select your aliases 

Note: Needless to add that you shouldn't perform any bulk generation action on your Drupal 8 site before you haven't backed up your database!

How to Generate Automatic URL Aliases in Drupal 8: bulk generate

6. Go Ahead and Delete Your URL Aliases If You Have To

Especially since removing them is nothing but a quick, two-step process:

  1. you click the “Delete aliases” tab
  2. you select whether it's all the aliases that you need to delete or just a specific entity type (user, taxonomy terms, content etc.) instead

How to Generate Automatic URL Aliases in Drupal 8: delete options

Note: Do keep in mind to check the “Only delete automatically generated aliases” box (in the “Delete Options” section), thus ensuring that your manually created aliases won't get deleted as well. 

Now all you need to define is the token to be used in generating your aliases: hit the “Browse available tokens” tab.

And this surely isn't an easy decision to make with such a “challenging” collection of tokens to choose from (after figuring out, of course, what each one of them actually does).

And this is how you do it! This is how you generate automatic URL aliases in Drupal 8 using the Pathauto module!