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:
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.