.htaccess – force trailing slash for url

This little .htaccess code does the trick on solving the SEO problem of bad url’s… let’s say that you have a https://www.example.com/images/ url that works ok, but someone forgets to add the last trailing slash and he/she accesses the https://www.example.com/images that, normally gives you a 404 error page and not the real stuff like photos and videos…

You just need to add this little code to your .htaccess file and if anyone forgets to add the last / after the url, it will be forced redirected to the right url with the / at the end.


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