How to redirect a URL always with slash (/) in the end?

1

I have this code:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteRule . /index.php [L]
</IfModule>

When I type http://localhost/path I'd like it to redirect to: http://localhost/path/

I would like to do this without changing my code base:
In the above code when you put http://localhost/path it redirects to index.php (I want to keep this). I want my code to be flexible to accept any URL changes and to accept both http and https.
I also want it to detect the folder that my site is in case it changes folder. Example: From localhost/ to localhost/www/site
And I do not want it to ignore the path of existing files and directories.

How to do this?

    
asked by anonymous 14.09.2014 / 21:00

1 answer

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

Lines with !-f and !-d cause the rule to not work if it is an existing file or directory path.

You need to see in your case if this is what you want when dealing with static images and files. Extra rules may be required, depending on the desired result.

    
14.09.2014 / 22:40