Why URLs Matter and How to Set Up Automatic URL Generation in Drupal

Dec 01 2021

A web page’s URL is much more than a bunch of symbols in the address bar. It can literally be a path by which both search engines and visitors come to you. 

In Drupal, every content page has a URL path that looks something like “/node/123” by default. This is definitely not something to be found by. Of course, URL paths can be edited manually, but how much time and effort can this process take, especially on content-heavy websites? The mission would look hard or sometimes impossible, but, luckily, there are great tools for automatic URL generation in Drupal. 

In this post, we discuss what makes a good URL, what exactly properly created URLs can give you, and how to auto-generate URLs on a Drupal website in a consistent way based on flexible patterns.   

The characteristics of good URLs

  • A URL should be descriptive so it’s clear what the web page is about. According to Google, it needs to be “most intelligible to humans” and contain readable words rather than long ID numbers. 
  • SEO-friendly URLs contain keywords that are relevant for the web pages.
  • Google also emphasizes the importance of making the URL structure of a website as simple as possible. The URL structure should logically describe the website’s structure.
  • The search giant recommends using punctuation in URLs to separate words. In punctuation, hyphens are preferable to underscores. According to Google, an example such as http://www.website.com/garden-tools.html will be more useful for indexing than http://www.website.com/gardentools.html
  • An important characteristic of a good URL is brevity. According to Serpstat, the appropriate maximum URL length is 75 characters.

Why optimized URLs are important for a website

  • Better SEO

URLs with query keywords and a structured URL hierarchy on a website can improve your website’s position in search. Consistent URLs make it easier for GoogleBot to crawl pages.

  • Attractiveness for users

Descriptive, concise, and consistent URLs are pleasant to read and understandable. That’s why they are attractive for potential visitors. 

  • Improved navigation

Optimized URLs that reflect the website’s structure can significantly improve navigation. URLs become part of menus and breadcrumb chains making it easy for users to find things and reach their goals on your website. 

  • Website promotion

Friendly URLs are convenient to share on social media or other websites and easier to remember for potential next visits.

When it’s better to optimize the URLs

It should be noted that the perfect time for creating optimized URLs is before launching your website so every page has the right URL from the start. This will free you from the hassle of redirecting the URLs when they are updated. However, it’s never too late to optimize the URLs on an existing website while taking good care of the redirect issues. In addition, URL restructuring is an inevitable part of website redesign and migrations to new platforms. 

How automatic URL generation works in Drupal 

Generating URLs automatically on Drupal websites is possible for both new and existing pages thanks to certain tools and modules, including Pathauto and Token Drupal.

The roles of the Pathauto and the Token Drupal modules

The Pathauto contributed module in Drupal is not a required tool to create URL aliases — the Drupal core also allows you to do that. What the Pathauto module does is provide automation for the URL generation process to free you from tedious manual work and eliminate human error. 

It enables you to define any preferred patterns for automatic URL generation on a Drupal website. You can easily get URL structures such as "website.com/blog//blog-title," "website.com/blog/category/blog-title," or any others based on your requirements. It works with Drupal content nodes, users, taxonomy terms, and media, and each entity type can have its own URL generation pattern. Using the Bulk generate option, you can automatically generate aliases for new content or regenerate ones for existing content. 

The Token Drupal module works together with the Pathauto module to help you use placeholders in the URL patterns while having the dynamically changing values in the actual URLs. For example, each specific blog’s URL can include its own blog title once you have added the [node:title] token to the automatic URL generation pattern. 

Guide on how to generate URLs automatically in Drupal

1. Installing the modules

First off, we need to install the Pathauto, the Token, and the Chaos Tool Suite (CTools)

Drupal modules. In order to have all dependencies installed without hassle, the preferred way is to use Composer:

$ composer require drupal/pathauto

2. Creating automatic URL patterns

The next step is to define the structure of the URLs by creating patterns. There are 3 ways to get to the page with the Pathauto patterns (and other settings):

  • The Pathauto module on the Extend tab > Configure 
  • Configuration > Search and metadata > URL aliases > Patterns
  • The content edit form > the “URL path settings” fieldset to the right

Once we are there, let’s click “Add Pathauto pattern” and create one for the blogs:

  • Under Pattern type, we select the Drupal entities we are creating this pattern for. In our case, it’s Content. 
  • Under the Path pattern, we specify the unchanging part for the URL that all items will include. It will be “blog/” for us.
  • To add other elements to the automatic URL generation pattern, we click “Browse available tokens,” find the node title token in the Nodes section, click on it, and it gets inserted automatically. 
  • Under Content type, we can restrict the pattern to the nodes of a specific Drupal content type only. Our pattern starts with “blog/” so it will be a great fit for blog nodes only. 
  • Finally, let’s give the new pattern a label. This is just a short name to help our website admins identify and find it. Then we click “Save.” 

It’s ready, but what about making it a little more customized by adding the blog category to the automatic URL generation pattern? Here we have a taxonomy vocabulary added to the blog content type as a Drupal taxonomy field named “Category.”

It’s ready, but what about making it a little more customized by adding the blog category to the automatic URL generation pattern? Here we have a taxonomy vocabulary added to the blog content type as a Drupal taxonomy field named “Category.”

It’s ready, but what about making it a little more customized by adding the blog category to the automatic URL generation pattern? Here we have a taxonomy vocabulary added to the blog content type as a Drupal taxonomy field named “Category.”

To include it, we can come back to the Pathauto pattern, find the category field in available tokens in the Nodes sections, and add it to the structure as well just before the blog title, separated with a slash. 

To include it, we can come back to the Pathauto pattern, find the category field in available tokens in the Nodes sections, and add it to the structure as well just before the blog title, separated with a slash. 

3. Reviewing the Pathauto settings

The Pathauto Settings tab allows us to specify some more general details that apply to all patterns. These include the separator character (“-” by default), the maximum alias length, the maximum component length, the omission of prepositions and articles in URLs, and more.

An important setting is under Update — we can choose what to do with old aliases during the automatic alias update. 

  • The default setting is “Create a new alias. Delete the old alias.” The content will no longer be available at its old aliases. 
  • If we select “Create a new alias. Leave the existing one functioning,” we will have multiple aliases for the same content. The Redirect module will then be needed to redirect old aliases to the new aliases.
  • We can also select “Do nothing. Leave the old alias intact.” 

Please note that while working with a website in production that already has content, it’s better to back up the database before playing with the Drupal Pathauto settings. 

4. Regenerating URL aliases for existing content

By using the Bulk generate tab on the Pathauto page, we can update the aliases of existing pages. Just select “Content” and they will be updated based on the Pathauto settings. The Pathauto also allows us to choose whether to update the URL aliases only un-aliased paths, ones with an old alias, or just all items. By clicking “List,” we will see all the nodes with the newly generated aliases and their system links (e.g. node/123). 

By using the Bulk generate tab on the Pathauto page, we can update the aliases of existing pages. Just select “Content” and they will be updated based on the Pathauto settings. The Pathauto also allows us to choose whether to update the URL aliases only un-aliased paths, ones with an old alias, or just all items. By clicking “List,” we will see all the nodes with the newly generated aliases and their system links (e.g. node/123). 

5. Flushing caches

An operation like updating URL aliases requires cache flushing. We can use either “Flush all caches” at the top left of the Drupal admin dashboard or the “drush cr” command in the command-line interface. 

6. Testing the content creation page

Let’s see what has changed on the Drupal blog creation interface now that we have automatic URL generation enabled. In the right part of the content creation page, there is the “URL path settings” option. It should get a new checkbox “Generate automatic URL alias” that is now checked by default. If it stays checked, aliases will be generated for all new content. Whenever content editors want to create a custom alias, they can uncheck the automatic generation and enter the URL path manually. 

Let’s create a new blog post to see if the right URL alias will be generated automatically based on our pattern. And yes, the “New Trends in Healthy Eating” blog post in the Food category has the following URL path with the category added and the prepositions omitted: 

blog/food/new-trends-healthy-eating

To sum up

Use the best practices of URL generation on your Drupal website and enjoy their benefits! Well-built URLs attract more visitors and search engines to your site while automatic URL generation gives you time and cost savings, consistency in URLs, and protection from human error. For any help with the setup, rely on our team of Drupal professionals. 

Learn from us
Sign up and receive our monthly insights directly in your inbox!