Bolt.tips

How to configure SEO-friendly URLs without ContentType

Define more SEO-friendly and pretty URLs that do not contain the Content Type of your record. For example /about-us instead of /page/about-us, but beware: if two records of a different Content Type have the same slug, you'll never know for sure which one will be visible on the frontend. For instance, if there is /page/about-us and /entry/about-us, then there's no way to objectively guarantee which one of the two items will show up on the shortened /about-us URL.

To configure Bolt to work with the shortened URL, you'll need to modify two files: routes.yaml and contenttype.yaml. The configuration depends on whether or not your site is multilingual.

Configure short URLs for a single-language website

  1. Locate your config/routes.yaml file and underneath the line # Place your own routes here, that have a LOWER priority than the default routes, add the following:
    content_seo_default:
    path: /{slugOrId}
    methods: [GET|POST]
    defaults:
        _controller: Bolt\Controller\Frontend\DetailController::record

This will make sure that the URL is properly matched.

  1. Configure Bolt to use that route internally as well as using the |link filter. To do this, add record_route: content_seo_default to all Content Types in config/contenttypes.yaml, for instance:
entries:
    name: Entries
    singular_name: Entry
    record_route: content_seo_default
    # the rest of the Content Type definition...

Note: At this time, the auto-generated links will contain ?contentTypeSlug={yourContentType} appended to them.

Configure short URLs for a multilingual website

  1. Locate your config/routes.yaml file and underneath the line # Place your own routes here, that have a LOWER priority than the default routes, add the following:
    content_seo:
    path: /{_locale}/{slugOrId}
    methods: [GET|POST]
    defaults:
        _controller: Bolt\Controller\Frontend\DetailController::record

This will match localized URLs, like /en/about-us and /nl-over-ons. You can also enable a short URL without the locale for your default locale, by adding a second route underneath:

content_seo_default:
    path: /{slugOrId}
    methods: [GET|POST]
    defaults:
        _controller: Bolt\Controller\Frontend\DetailController::record
  1. Configure Bolt to use that route internally as well as using the |link filter. To do this, add record_route: content_seo_default to all Content Types in config/contenttypes.yaml, for instance:
    entries:
    name: Entries
    singular_name: Entry
        locales: ['en', 'nl']
    record_route: content_seo
    # the rest of the Content Type definition...

I18n ContentTypes Seo

Permalink - Written by Ivo on January 15, 2021.

« Fixing JSON errors after database conversion from MySQL 5.6 to 5.7 - Outputting the total number of Records for a contentType »