How to include the collection in the URL?

Greetings, I have tweaked the routes file to remove the /tag segment from the URL, and it works great for collections. However, when I view a post, the URL only includes the post segment without the collection it belongs to. For example, example.com/my-post appears instead of example.com/collection/my-post. Is it possible to show the collection segment before the post segment?

Thanks!

You can set the permalink in the routes file to something like
/{primary_tag}/{slug}, or specify each collection separately, each with a hard-coded collection name.

Hi, i tried that, got an error.

So did you add a trailing slash? :)

Yes i added, and the restart worked, but the URL structured didn’t, the URL for posts start directly after the TLD, and after the changes in the routes.yml file the navigation menu is broken.

If you’d like to post your routes file (be sure to use the code button so it’ll be readable), I’m happy to take a look. It should work, and it should not require a restart either, as long as you’re uploading via the ‘labs’ part of settings. This does assume that your posts have tags, and that the primary (first) tag is NOT internal (does not start with #).

Yes, changing the URLs of your posts will break navigation links that you previously set up. You can edit your links, or write a redirect file.

This is how the routes.yml file look

Ah, OK, so it needs to be:

routes:

collections:
  /: 
    permalink: /{primary_tag}/{slug}/
    template: index

taxonomies:
  tag: /{slug}/
  author: /author/{slug}

The collections part determines the URL of the post. This will cause the primary tag to appear in the post URL. (You’ll need to make sure you have a primary tag set, and not starting with #).

The taxonomies determine the urls for the tags page and author pages. Currently, it’ll use /politics/ to show all posts with the tag ‘politics’ (in any position, not just first), and /author/sam to show all posts by the author whose slug is ‘sam’.

A single post (titled ‘Your Post’) with the primary tag ‘politics’ will show up at /politics/your-post

If that’s not what you were trying to do, please explain where that’s not matching what you wanted, as perhaps I’ve misunderstood. :slight_smile:

1 Like

No, it works perfectly now. That was my mistake—I saw the tag: key and thought that was where I should add it. Got it now. Thanks, mate!

1 Like