无法添加尾随 /

Unable to add trailing /

我是设置 .htaccess 文件的新手,非常感谢您对这个问题的帮助。

我无法将结尾 / 添加到我网站的所有 URL。我已经尝试了 Whosebug 和这篇文章中的大部分答案,但它对我不起作用,要么样式损坏并且没有添加 /,要么在尝试这些解决方案时只是没有添加 /

可能规则有冲突?

这是我的 .htaccess 现在的样子:

# do not allow anyone else to read your .htaccess file
<Files .htaccess>
deny from all
</Files>

# forbid viewing of directories
Options All -Indexes

# hide this list of files from being seen when listing a directory
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

# disable the server signature- helps with preformance
ServerSignature Off

RewriteEngine On
RewriteBase /

# specific rule to show 1 URL but other URL is active
RewriteCond %{DOCUMENT_ROOT}/\.html -f [NC] 
RewriteRule ^([^/]+) .html [L]

# hide /index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

# add trailing /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !http://example.com
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com// [L,R=301]

我想将尾随 / 添加到除 http://example.com 之外的所有 URL(我正在设置默认值 /index)。

我该如何解决这个问题?

你的最后一个看起来不对,而且你的规则顺序错误。试试这个代码:

# do not allow anyone else to read your .htaccess file
<Files .htaccess>
deny from all
</Files>

# forbid viewing of directories
Options All -Indexes

# hide this list of files from being seen when listing a directory
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

# disable the server signature- helps with preformance
ServerSignature Off

RewriteEngine On
RewriteBase /

# hide /index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index($|\ |\?)
RewriteRule ^ /%1/ [R=301,L,NE]

# add trailing /
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^[^.]+$ %{REQUEST_URI}/ [L,R=301,NE]

# specific rule to show 1 URL but other URL is active
RewriteCond %{DOCUMENT_ROOT}/\.html -f [NC] 
RewriteRule ^([^/]+) .html [L]