.htaccess中的强制尾随斜杠不起作用

时间:2015-02-10 01:16:21

标签: apache .htaccess redirect

除了在子目录中有一些Wordpress网站(在本网站上没有引用)之外没有做任何事情,链接会自动从他们自己剥离.html结尾。我不知道为什么会这样,但这是我想要的选择。

现在,我在尝试强制在我的网站中使用斜杠时遇到了麻烦。我尝试了这个选项:

Options -Multiviews
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

我目前也将它用于404页面(引用404.html):

ErrorDocument 404 /404

但是,在尝试访问某个页面时,例如http://bluecocoa.com/time/,我遇到了重定向无限循环错误。我使用http://redirectdetective.com跟踪重定向。我发现重定向是这样的:

http://bluecocoa.com/time --> http://bluecocoa.com/time/ --> http://bluecocoa.com/404 --> http://bluecocoa.com/404/ --> http://bluecocoa.com/404 --> http://bluecocoa.com/404/ --> infinite loop

对我来说,看起来它通过添加一个来修复没有尾部斜杠的页面。由于某种原因,找不到具有尾部斜杠的页面,并且它重定向到404. 404导致无限重定向。

我的网站目录如下:

index.html
time.html
/mtc/ [a wordpress directory]
/posts/
blog.html
etc...

非常感谢您解决这些问题的任何帮助!

1 个答案:

答案 0 :(得分:1)

使用这些规则隐藏根.html中的.htaccess扩展程序:

Options -MultiViews
RewriteEngine On
RewriteBase /

# To externally redirect file.html to /file/
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=302,NE,L]

# To internally forward /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
相关问题