.htaccess rewrite trailing slash condition for specific directories only

I have this rule that works fine and adds a trailing slash for every directory on my site. How to add trailing slashes only for certain directories, such as / courses /? I do not want each directory / path to have a trailing slash.

  RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
  RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
+3
source share
2 answers
RewriteCond %{REQUEST_URI} ^/(courses|foo|bar)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

Should work where courses, fooand barare the directories to which you want to add the trailing slash.

+8
source

Think this will work:

<Files .htaccess>
    order allow,deny
    deny from all
</Files>

Options All -Indexes
IndexIgnore *

# Don't add trailing slash for /include/
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/include/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1

# Respond to /include/ with 404 instead of 403
RedirectMatch 404 ^/include(/?|/.*)$
0
source

All Articles