简体   繁体   English

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

[英]Force trailing slash in .htaccess doesn't work

Without doing anything excepting having some Wordpress sites in subdirectories (not referenced on this website), the links automatically strip the .html endings from themselves. 除了在子目录中有一些Wordpress网站(此网站未引用)外,这些链接无需进行任何操作,这些链接会自动从自己身上删除.html结尾。 I don't know why this is, but it is the option I want. 我不知道为什么会这样,但这是我想要的选项。

Now, I'm having trouble trying to force trailing slash in my site. 现在,我在尝试在网站中强制使用斜杠时遇到了麻烦。 I tried this option: 我尝试了这个选项:

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

I currently also use this for 404 pages (referencing 404.html): 我目前也将其用于404页(引用404.html):

ErrorDocument 404 /404

But, when trying to access a page, such as http://bluecocoa.com/time/ , I got a redirect infinite loop error. 但是,当尝试访问页面(例如http://bluecocoa.com/time/)时 ,出现了重定向无限循环错误。 I traced the redirects using http://redirectdetective.com . 我使用http://redirectdetective.com跟踪了重定向。 I found that the redirects went like this: 我发现重定向是这样的:

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

To me, it looks like it fixes the page without the trailing slash by adding one. 对我来说,它看起来像是通过添加一个来固定页面而没有尾随斜杠。 For some reason, the page with the trailing slash isn't found, and it redirects to 404. The 404 causes an infinite redirect. 由于某些原因,找不到带有斜杠的页面,它会重定向到404。404会导致无限重定向。

My website directory looks like this: 我的网站目录如下所示:

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

Any help in fixing these issues would be greatly appreciated! 解决这些问题的任何帮助将不胜感激!

Have these rules to hide .html extension in your root .htaccess : 具有以下规则可在根.htaccess隐藏.html扩展名:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM