The code to implement an SEO-friendly, unique slug in dotCMS is provided below. Note that this code is for dotCMS 1.9.x series, but it may also work with future versions. It consists of two pieces:
- Friendly slug custom field. This code is entered as a custom field in a content structure. In this example, the field is called “slug,” but some developers may prefer to use “urlTitle” or even “id.” The first two lines of code must be modified. First, $customVarSource must be set to the field to use to generate the slug (e.g., “title“, “headline,” or “name“). Then, $friendlySlugURL must be set to the relative address of the page containing the friendly slug JSON service (from step 1).
Friendly slug JSON service code:
Friendly slug custom field code: