How to Add Trailing Slash “/” to Joomla URLs Without .htaccess

I wanted to add a slash “/” to the end of all the URLs on my install of Joomla 3.3.1. So that all the pages on the site would have an address like “http://www.domain.com/page/”. As default, Joomla creates URLs without the trailing slash, ex. “http://www.domain.com/page”.

Joomla Trailing Slash

The post has been updated to work with Joomla 3.8.x and beyond!


This is possible to achieve with .htaccess, in my case however, I could not get it to work by pasting code suggested on the web to the default Joomla htaccess.txt file.

Luckily I found another workaround here, which I prefer anyway.

The htaccess method will show links as “xx/page” and simply forward them to -> “xx/page/” per request. But this added code will add the trailing slash into the Joomla code itself, creating clean, working URLs for your website.

Joomla URLs with trailing slash

We need to edit the site.php file. Do a back up first, and open up this file with your favourite text editor:

/home/user/public_html/libraries/cms/router/site.php

The file is now SiteRouter.php, since Joomla 3.8.x;

/home/user/public_html/libraries/src/Router/SiteRouter.php

Look for the line “Set query again in the URI“, around line 573.

And replace the code:

// Set query again in the URI
$uri->setQuery($query);
$uri->setPath($route);

With:

// Set query again in the URI
$uri->setQuery($query);
$uri->setPath($route . '/');
}

That should fix it. The downside is, that if a future update of Joomla replaces the file with a new one, and you will need to do this again.

Please, let me know if it worked for you.
Or leave your questions in the comments! David.

10 thoughts on “How to Add Trailing Slash “/” to Joomla URLs Without .htaccess”

  1. Hello, great it works thank you very much! I have installed joomla local with xampp. But allow me this question – is it possible to get the trailing slash by joomla menu item type External URL, too? I have in my main menu 2 such menu types. For example my local domain is just yet http://localhost/en/, my external url is https://www.motorsport.com/
    By mouseover on the external url within Firefox 54 it does not show the url preview with the trailing slash in the bottom left of the browser window? All other internal urls are showing with “/” at the end in the url preview. I would be very pleased for an answer. Kind regards Karin

    Reply
  2. In 3.8.x the site.php has been moved and renamed.
    /home/user/public_html/libraries/src/Router/SiteRouter.php – starting at line 573

    Reply
    • I’ve changed .htaccess by adding the entry which adds the trailing slash. Since OSMap can’t be configured to add the trailing slash thus creating a lot of duplicate content I decided to use the option you are describing. It works perfectly.

      Reply
  3. Hello,

    in Joomla 4 how to add a trailing bar?

    I tried adding this code in the file /public_html/libraries/src/Router/SiteRouter.php

    // Get the path
    if ($ tmp) {
    $ uri-> setPath ($ uri-> getPath (). ‘/’. $ tmp. ‘/’);
    }

    inside the buildSefRoute function

    Reply

Leave a Comment