Add trailing slash


Is it possible to write a rule that will add a slash to a URL that someone enters?

My website is setup like this:

If someone enters the page will not load. the page loads correctly.

There is an index file in each folder.

I have tried this but it does not work.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$$1/ [L,R=301]


Actually, your server should be setup to determine whether the request is a directory and then select the DirectoryIndex from that directory. In other words, complain to your host (first).

Then, to show your host that you’re smarter than they are, REMOVE the useless RewriteBase directive, test for the request being a directory THEN redirect to add the trailing /, i.e.,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .?{REQUEST_URI}/ [R=301,L]

When you create a specification, you can see the clearest path to actually writing the code. You wanted to add the trailing / to a directory request so that should have been your test and everything else just falls into place.