Adding a Trailing Slash to all URLs in Magento 2

Adding a Trailing Slash to all URLs in Magento 2

July 31, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Adding a Trailing Slash to all URLs in Magento 2

Adding a Trailing Slash to all URLs in Magento 2

If you’re trying to add trailing slashes to your Magento 2 URLs, you’ve likely discovered that the default configuration removes them. This can cause issues with SEO and site performance if not handled correctly. Let’s walk through how to achieve this without causing excessive redirects or slowing down your site.

Understanding the Default Behavior

By default, Magento 2 uses canonical URLs which remove trailing slashes for SEO purposes. The issue arises when you want to serve content from both https://example.com/category/ and https://example.com/category.

The Inefficient Approach: .htaccess Rewrite Rules

You might attempt to add the following rules to your .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R]

While this might seem logical, it can create a large redirect chain. The R flag causes an external redirect which triggers the rewrite rule again on the new URL. This loop continues indefinitely, causing excessive redirects and significant performance degradation.

The Efficient Approach: Magento’s Core Configuration

Instead of modifying the .htaccess file directly, we can utilize Magento’s built-in functionality to add trailing slashes. Here are the steps:

  1. Open your terminal and navigate to your Magento 2 installation directory.
  2. Run the following command to enable trailing slash URLs in the default configuration:
php bin/magento config:set system/seo/url_trailingslash/yes
  1. To apply this change, you need to deploy static content:
php bin/magento setup:static-content:deploy
  1. Finally, clear the cache to ensure the changes take effect:
php bin/magento cache:clean

Best Practices for Trailing Slash URLs

  • Consistency is key: Ensure all your URLs follow the same trailing slash convention.
  • Canonical URLs help SEO: While they remove trailing slashes by default, using proper canonical tags can help Google understand your preferred URL structure.
  • Consider performance: If you have a large site with many URLs, adding trailing slashes to every page may increase resource usage. Weigh this against the benefits for your specific use case.

Conclusion

Adding trailing slashes to Magento 2 URLs requires leveraging Magento’s built-in configuration rather than modifying the .htaccess file directly. By following the steps outlined above, you can achieve consistent URL structures without causing excessive redirects or performance degradation.

Remember, every site is unique. If you have questions about your specific setup, don’t hesitate to reach out to a professional developer. At Laravel Company, we specialize in Magento development and optimization strategies tailored to your business needs. Contact us today for expert guidance on optimizing your e-commerce platform.

Visit our website [https://laravelcompany.com] for more insights into Magento 2 development best practices.

Magento 2 Catalog Performance Configuration

Our network