When building a website with Statamic, managing and customizing routes for taxonomies can enhance both the user experience and the site's SEO. By default, Statamic provides a straightforward way to handle taxonomy routes, but there are also options for more advanced customization. In this post, we'll explore how to specify and manage taxonomy routes in Statamic, including leveraging Laravel's routing capabilities and using addons for even greater flexibility.
Default Taxonomy Routing
Statamic automatically creates routes for taxonomies, which simplifies the process of organizing content. Here's how these default routes are structured:
Global Taxonomy Details:
Taxonomy URLs are structured using slugs with dashes, while views use handles with underscores.
The taxonomy can be accessed at
/{taxonomy-slug}
(e.g.,/tags
), with the view located at{taxonomy_handle}/index
(e.g.,tags/index.antlers.html
).
Global Term Details:
Individual terms within a taxonomy are accessible at
/{taxonomy-slug}/{term-slug}
(e.g.,/tags/t-shirts
).The view for terms is
{taxonomy_handle}/show
(e.g.,tags/show.antlers.html
).
Collection-Specific Taxonomy Routes:
If a taxonomy is assigned to a collection, routes are also available at
/{collection-url}/{taxonomy-slug}
(e.g.,/products/tags
).For specific terms, the route would be
/{collection-url}/{taxonomy-slug}/{term-slug}
(e.g.,/products/tags/t-shirts
).
These default routes are sufficient for many use cases, but there are scenarios where custom routes are necessary for better integration or user experience.
Custom Taxonomy Routes
For developers looking to tailor their site's routing structure, Statamic provides options to define custom routes. Here are two popular methods:
Custom Routes with Laravel
Statamic is built on Laravel, which means you can leverage Laravel's powerful routing capabilities to define custom routes. This is done in the routes/web.php
file. Here's an example of how to define a custom route:
Route::statamic('custom-uri', 'view-name');
This approach allows you to create unique URLs that align with your site's structure and branding.
Using the Bonus Routes Addon
The "Bonus Routes" addon is a handy tool for creating custom taxonomy routes without diving deep into Laravel's routing. It provides a simple syntax for defining routes:
Route::bonus('taxonomy:topics', 'categories/{slug}', 'topics.show');
This method is particularly useful for mounting taxonomies to entries and customizing taxonomy URLs to be more descriptive and user-friendly.
Conclusion
Customizing taxonomy routes in Statamic can significantly enhance your site's functionality and user experience. Whether you stick with the default routing or opt for custom routes using Laravel or the Bonus Routes addon, Statamic offers the flexibility needed to create a seamless and intuitive navigation structure. By tailoring your taxonomy routes, you can better align your website with your content strategy and improve overall site performance.