1

I would like to enforce a trailing slash on all URLS except on filenames. Here is my .htaccess file so far.

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
jmoerdyk
  • 5,544
  • 7
  • 38
  • 49

2 Answers2

0

Simple, check for the condition that the requested file doesn't exist, and redirect:

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

Note that the above rule set will go just before your THE_REQUEST check.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Thanks for your answer. Ran into one problem... When I go to: https://example.com/directory/index.php It redirects to: https://example.com/directory // (an extra slash) Any way to prevent that ? –  Sep 09 '16 at 14:38
0

Have your htaccess like this:

RewriteEngine On

# this rule will do 3 things: http->https, www removal, add a trailing slash 
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{REQUEST_URI} !/$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1/ [R=301,L,NE]

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [R=301,L,NE,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643