All Drupal nodes have a specific default system URL when they are created. For example, the user URL pattern is /user/<user_id> and when a node of any type is created the URL pattern is /node/<node_id>. It is highly recommended to mask the paths of your website by creating aliases that are applied on top of the system paths. The aliases are useful for the following reasons:
- If the URL pattern is descriptive, then it is easily memorised.
- Text-based URLs are better for Search Engine Optimisation they include words, which are easier indexed by search engines.You can see all the aliases by visiting /admin/config/search/path.
An automated way for masking the system paths is by creating your own custom URL patterns. For the creation of your patterns, we propose the following suggestions:
- When you create a content type, it is recommended to keep the same path pattern for the nodes of the same content type. For example, if you have a content type called Article, you can create a pattern for this content type which will follow the following pattern /article/<title_of_the_article>. As a result, when you create a node of type Article, for example an article entitled “Latest News at CERN”, the URL of the node will be: /article/latest_news_at_cern.
- If you tag your content with taxonomy terms to categorise them, it is better to include this taxonomy in the URL. This way, the URL will be even more specific and easier memorised. For instance, if you have an articles’ category called “Experiments”, you can specify the content being tagged with this category to appear in the URL. In the end, the URL will be /article/experiments/latest_news_at_cern.
Of course, you can use whatever name you prefer, since the path “/article/” and “/experiments/” were used as examples.
The mask does not replace neither duplicates the node. It’s just an alternative, more descriptive and memorisable way to visit a webpage. The system path exists even after the creation of a mask. You can personally test the latter by finding the node ID of a page (Ex. /node/39) and then creating a mask for a page (ex. /article/cms). You will notice that both /node/39 and /article/cms lead to the exact same node.
How to create a path pattern
Path patterns are generated by the Pathauto module. When you create a CERN website, by default the Pathauto module is installed but not enabled. As a result, in order to start creating and generating paths, you first need to enable the module and clear the caches. If for any reason the module is not installed, you can always find it in this link (https://www.drupal.org/project/pathauto). In order to create a path pattern in Drupal 8, you first need to visit /admin/config/search/path and click on the “Patterns” tab and follow the following steps:
[In the following steps we assume that we create a pattern for the “Article” content type]
- Click on “Add pathauto pattern”
- Fill the fields
- Pattern Type: Select a pattern type which can be either content, taxonomy term or user.
- Path Pattern: Enter the path pattern that you wish the nodes of the content type(s) to have. You can also use tokens, which have the ability to take a dynamic value (You can check the available tokens by clicking “Browse available tokens”).
- Label: it with a descriptive name in order to be more visible within the list of patterns. In our case let’s name it “Articles’ pattern”.
- Language: check the language that you want this pattern to be applied. This option is useful if you want different words for different languages.
How to generate paths for the entities
Having created the desired patterns for your content, taxonomy terms etc, you can then generate the aliases for of the entities. In order to do this:
[For the following steps be extra careful and sure that you know what you are doing. Changing the URLs of a website might lead to losing SEO indexes and/or losing multiple links to your website. Just to be sure, always keep a backup and first apply the changes on a test website]
- Visit again /admin/config/search/path/patterns
- Click on Bulk Generate
- Fill the fields of the form
- Select what types of paths you need to generate: content, taxonomy terms or users. The generated paths are of course based on the created patterns of the previous section.
- Select which URL aliases to generate: The available options for this case are:
- Generate a URL alias for un-aliased paths only: Using this option will generate an alias only for the system paths that are not already masked with an alias.
- Update the URL alias for paths having an old URL alias: Using this option will generate an alias only for paths that are already masked. It will not generate for system paths that do not have a mask
- Regenerate URL aliases for all paths: This option will regenerate URL aliases for both unaliased and already aliased system paths.
- Click on Update and...Done! Make sure you clear the caches after the generation.
A reasonable question that someone might have when creating node patterns is: What happens if you create a pattern using tokens but the value of the token is empty? Let's use an example to make it a bit more clear and say that we have a pattern for the content type Articles which are tagged with the category taxonomy terms (ex. Experiments etc.) and the category field is optional. The pattern that we created is as follows (using tokens): /articles/[node:field_category]/[node:title]. What will happen if a node is not tagged with the category, since this field is optional? In this case the middle url element will be skipped. In other words, if the node title is for example “CMS” the final url mask will be /articles/cms/.