I have the following code in my htaccess file:
# Force Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
That seems to work fine when I go to www.mydomain.com/test it redirects it to /test/. The problem is when I go to www.mydomain.com/test/another it doesn't put the trailing slash on another.
Does anyone know how to modify my code to make the trailing slash work no matter how long the URL is?
Thanks!
A slightly more robust answer, based on the answer above:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
The RewriteCond
will check to make sure there's no files with that name, and if not, perform the RewriteRule. More future-proof than having a manual list of extensions!