Credits & Thanks
Thank you to:
- Dave Reid for creating this module
- Sascha Grossenbacher (Berdir)
- The large number of other contributors to this module!
About Tokens and the Token System
To fully take advantage of patterns, you need to understand a little about the Token system.
Tokens are variables in Drupal. There are thousands of Tokens available for you to use. To get a UI in the admin area for browsing all available tokens (and have tokens that aren’t available in core), then you’ll want to have your developer install the Token module.
To see what they are and get a better understanding of how they work, you can visit https://<yourDrupalSite.dev>/admin/help/token.
Bear in mind that not all of the tokens are available in all areas. For now, we are going to focus on just a handful of critical Tokens that we’ll use to create our URL path patterns. They are:
[node:title] - The title of the piece of content being displayed.
[term:vocabulary] - The vocabulary (top level category, so to speak) of the current taxonomy page.
[term:name] - The name of the current term (bottom level category).
Whenever Drupal sends a page to a visitor, it first replaces the tokens with the corresponding text. i.e. the “Today” token might be replaced with “February 22, 2017” or “August 26, 2018”.
Don’t worry if this doesn’t make sense yet. What you need to know right now is that we’re going to tell Drupal to create some paths for us, and we’re going to use Tokens to make it happen.
Create Pathauto Patterns
Drupal URL paths operate in patterns. Instead of creating a path to every single piece of content, it’s better to specify a pattern (using tokens) for groups of content. Drupal will follow the pattern to create the path for each new piece of content, ensuring consistency across your website.
You’re going to add a Pathauto pattern for each Content Type and taxonomy that you have.
- Go to the URL aliases page: Click Manage > Configuration > Search and metadata > URL aliases (Coffee: url aliases) or or visit https://<yourDrupalSite.dev>/admin/config/search/path/patterns.
- Click the Patterns tab.
- Click the + Add Pathauto pattern button.
- From the Pattern type drop-down, select Content. The form will update to reflect that content type’s settings.
Note: Leave existing path patterns as they are unless you have a good reason to change them. Any changes to these settings will not change existing pages, only pages created moving forward and could create issues with your content siloing efforts.
- Fill out the fields as shown.
- Path pattern: “[node:title]”
- Content type: select “Article” and “Basic page”
- Label: Anything goes. We use something descriptive such as “Content Types: Article & Basic”
- Select the Enabled checkbox.
- Click the Save button near the bottom of the page. The resulting page will look something like this:
- Repeat the above steps for each of your Content Types.
Note: When you create new content types, you’ll want to go through this process for each of those at that time.
Now, when you create new pages of these content types, the [node:title] will be replaced with a normalized version of the title of the content. Drupal will change the letters to lowercase, replace spaces with dashes, and remove any odd characters.
For example, if you create a page with the title of “Cat Pictures for the Cat Lover in All of Us” would get converted to something like this:
Going a step further with Pathauto
If you create your patterns like the example above, you will have a flat website with no hierarchy. If you created three basic pages called “Our Products”, “Our Team”, and “Our Customers” then you’d have three pages that look like this:
Maybe that’s what you want, but maybe you want something a little deeper. You can edit the patterns you’ve created or delete them and create new ones. For example, let’s say you’ve created a new Content Type for your blog called “Blog Postings” and you want them to be under the /blog directory. You’d create a Pathauto pattern that looks like this:
As you can see, you’d enter blog/[node:title] in the Path pattern field. Your blog URLs might look like this:
Or, if your blog focuses on a single topic (cats, is it?) then you might put this:
Which produces this:
Better! Now you’ve used the powerful key phrase “cat blog” which improves your SEO for that keyword. Now, when you create each new piece of content, it will be in the /cat-blog/ section of your website.
Now let’s update your taxonomy terms. Let’s say you create a new Pathauto pattern that looks like this:
You might be wondering about the Path pattern for the Taxonomy term.
[term:vocabulary] is the top level category that the term belongs to. In this case, Tags.
[term:name] is the name of the tag (i.e., the tag itself).
In use, it might look like this:
This is not an exhaustive look at tokens, but it should be a good start when conducting SEO for your website.
Did you like this walk through? Please tell your friends about it!