.htaccess rewrite condition trailing slash only for specific directories

◇◆丶佛笑我妖孽 提交于 2019-12-10 17:32:21

问题


I have this rule which works great and adds a trailing slash for every directory on my site. How Can I add trailing slashes for only certain directories such /courses/ ? I don't want every directory / path to have a trailing slash.

  RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
  RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

回答1:


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

Should work where courses, foo and bar are directories that you want to add a trailing slash to.




回答2:


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(/?|/.*)$


来源:https://stackoverflow.com/questions/6034824/htaccess-rewrite-condition-trailing-slash-only-for-specific-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!