删除除特定目录之外的尾部斜杠

时间:2015-08-08 03:25:42

标签: apache .htaccess mod-rewrite

我目前的代码是从主文件夹中的所有目录中删除尾部斜杠:

DirectorySlash Off

#removing trailing slash    
RewriteCond %{THE_REQUEST_FILENAME} \ /(.*)/(\ |$|\?)
RewriteRule ^(.*)/$ $1 [R=301,L]

# internally add the slash back
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L]

如何添加现有代码以排除文件夹" myfolder"?

感谢您的帮助! :)

2 个答案:

答案 0 :(得分:0)

您可以将规则设为:

DirectorySlash Off
RewriteEngine on    

#removing trailing slash    
RewriteCond %{THE_REQUEST_FILENAME} \s/+(.+?)/[\s?]
RewriteRule !^myfolder/$ /%1 [R=301,L]

# add trailing slash for myfilder
RewriteRule ^(myfolder)$ /$1/ [L,NC,R=301]

# internally add the slash back
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?[^/])$ /$1/ [L]

答案 1 :(得分:0)

RewriteEngine on

之前的文件中添加DirectorySlash Off
RewriteEngine on    
DirectorySlash Off

#removing trailing slash    
RewriteCond %{THE_REQUEST_FILENAME} \ /(.*)/(\ |$|\?)
RewriteRule ^(.*)/$ $1 [R=301,L]

# internally add the slash back
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L]