无法添加尾随/

时间:2015-02-22 13:42:15

标签: regex apache .htaccess mod-rewrite url-rewriting

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

我无法在网站的所有网址中添加尾随/。我已经在StackOverflow和本文中尝试了大部分答案,但它对我来说没有用,要么样式破坏了,没有添加/或者在尝试这些解决方案时没有添加/

规则中可能存在一些冲突吗?

以下是我.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}/$1\.html -f [NC] 
RewriteRule ^([^/]+) $1.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/$1/ [L,R=301]

我想向除/以外的所有网址添加尾随http://example.com(我正在使用默认/index)。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你的上一次看起来不正确,除了你的规则是错误的顺序。试试这段代码:

# 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}/$1\.html -f [NC] 
RewriteRule ^([^/]+) $1.html [L]