Solution: Forcing Trailing Slash Using .htaccess

From last one week I have been trying to fix an issue, forcing the trailing slash to the category pages of one of my websites. The website is built upon Magento, I’ve searched a lot in their forums and apache related websites but couldn’t find a solution which actually works. A lot of them are discussing the issue but not ending up with a solution to it. Today, somehow I managed to find a fix to the issue and thought of publishing it.

Magento Trailing Slash Issue:
I’ve posted an SEO review on the Magento e-commerce platform last week and I’ve been busy fixing each of those issues. Magento and most of the ecommerce websites display the category pages as directories. The URL displays like the following:

Suppose if someone requested for ‘’ – note: the trailing slash is not added -. There are two chances:

  1. Server returns a 404 page.
  2. Server displays the content of

In the first case, it’s clearly an error and most probably you are losing a good percentage of visitors.

In the second case, it will cause duplicate content threat in search engines and your pages will not get ranked for your target keywords.

Conclusion: We have to fix this, but how?

The solution is to create a .htaccess rewrite rule that adds the trailing slash to such URLs. Based on the server responds you get, choose from the following. Place the code inside your .htaccess file and check.

Note: Ensure you back up your .htaccess file before doing the update. The code may not be working if your configurations are different, please get in touch with your server administrator

  1. Server returns a 404 page.
  2. RewriteEngine  on
    RewriteCond    %{REQUEST_FILENAME}  -d
    RewriteRule    ^(.+[^/])$ $1/  [R]

  3. Server displays the content of
  4. RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !.html
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$$1/ [L,R=301]

Note: RewriteCond %{REQUEST_URI} !.html – actually ensures the rewrite will not affect the pages with a .html extension. You can change this to .php, in case you have .php extensions.

2 thoughts on “Solution: Forcing Trailing Slash Using .htaccess

  1. Thanks for handy info. Unless I’m missing something, this seems to solve only half the problem. What about the way Magento generates its own URL links? For example, Magento’s category breadcrumb links have no trailing slash. When a customer clicks on one of those links, the above rewrite rules will redirect the slashless request to one with a trailing slash. But the need to process the re-direct causes additional server load and delay (however small) for the customer. It would be far preferable if Magento could be coaxed into adding a trailing slash to all links appearing in the front-end. Does anyone know of a way to do this?

  2. Justin, happy to know that you liked it.

    Regarding the magento breadcrumbs, if you know the back-end file structure, try to locate the breadcrumbs.phtml – this one normally contains the code for your breadcrumbs. You can add the trailing slash to it to avoid the additional server load.

Leave a Reply