Drupal SEO Series using Metatag
- Getting Started with Metatag Module in Drupal
- Generate Twitter Card Meta Tags using Metatag in Drupal
- Generate Open Graph Meta Tags for Facebook using Metatag in Drupal
- Automatically Generate URL Aliases using Pathauto in Drupal
- Manage URL Redirects using Redirect module in Drupal
- Generate Sitemaps using Simple XML Sitemap in Drupal
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.
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.
Good tips in using this key module and very well presented.
Menu Structure as Path is great and it will be really useful and its new great work you made learning Drupal easy Ivan 🙂
Thanks.
Thanks.
Hi Ivan
i use drupal 8 with pathauto module, the language of my website is Greek, when i create new content the url alias is not in greek, any idea how to fix that
https://postimg.org/image/w8k5dck9r/
thank you
Hi Spiros,
I don’t know. Best to ask in the Pathauto issue queue. https://www.drupal.org/project/issues/pathauto
Cheers,
Ivan
Pls add info about tag patterns or about tags and pathauto 🙂
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
Yes, Taxonomy
Interest example patterns 🙂
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.
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
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
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
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
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?
Hi Daniel,
So you want to setup a redirect?
Look at setting up an event subscriber, https://www.thirdandgrove.com/redirecting-node-pages-drupal-8
Cheers,
Ivan
Is it possible to create a custom URL alias pattern using hook_pathauto() like we did in D7?
Thanks
P.G.
Hi,
I’m not sure if that specific hook is still in D8. But look in the pathauto.api.php file for all hooks.
Cheers,
Ivan
Hi Ivan, I’m a newbie to Drupal. I have just activated Pathauto. Once I finish configuring it, set to automatically generate aliases, what happens to the /node/xxx URLs? If we access to /node/xxx directly, will it redirect to the new URL?
Hi Hope,
If you’re using Drupal 8, then download and install the Redirect module (https://www.drupal.org/project/redirect).
Then configure the global redirects section, https://www.webwash.net/?p=4771#global-redirect, check “Enforce clean and canonical URLs” option.
Cheers,
Ivan
can we create alias for files url as well.
For example: http://www.example.com/sites/default/2018-16/content/files/1.jpg to http://www.example.com/resources/1.jpg
Hi Ashish,
Have a look at Sub-pathauto (https://www.drupal.org/project/subpathauto)
I’ve never used it but have a look at it.
Cheers,
Ivan
Have you seen an issue where the pattern is applied when testing in a local environment, but not on the server? I have this:
News Article news/[node:title]
For some reason, the “news/” is not added when a news article is created or updated on our production server.
Hi Shelane,
I’ve never experienced this type of issue. Do other tokens work or is it tokens just in Pathauto? That’s what I’d investigate next.
Cheers,
Ivan
Hello, I’m trying to create a path segment from taxonomy and don’t know what I do wrong: we have nodes with field “field_type_ref” referencing a vocabulary with types: “type1”, “type2” etc.
The desired paths are /type1/entity_title /type2/entity_title, but the “type” part of the alias remains empty. I tried to fetch it with: [node:field_type_ref:entity], [node:field_type_ref], even tried to show the term id: [node:field_type_ref:target_id], but it always results in an empty path segment. The [node:title] works though.
Hi Ivan,
I am currently on a D9 project and set pathauto successfully on my Staging env. Pls can you give me hints that how do I expose the URL alias (slug) value in REST API. TIA.
Sorry never done anything like that before.