简体   繁体   English

.htaccess删除.html扩展名,并添加斜杠并禁止.html文件

[英].htaccess remove .html extension and add trailing slash and disallow .html files

I have a static website with html files and I want to call them without the .html ending and a trailing slash. 我有一个带有html文件的静态网站,并且我希望在不带.html末尾和斜杠的情况下调用它们。

For example: 例如:

www.test.com/test.html

Should be available under 应该在

www.test.com/test/

I managed this with the following .htaccess 我使用以下.htaccess进行了管理

Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^([^\.]+)/$ $1.html

But both versions of the url lead to the same page. 但是,这两个版本的url都指向同一页面。 How can I redirect (without .html) or throw a 404 for the .html version? 如何重定向(不带.html)或为html版本抛出404?

www.test.com/test.html

This is the working solution with a trailing slash. 这是带有斜杠的有效解决方案。 Thank you @arkascha! 谢谢@arkascha!

Options -Multiviews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?(.+)\.html$ /$1/ [R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html [END]

Well, add another rule that explicitly redirects requests to URLs with "file name extensions"... 好吧,添加另一个规则,该规则将请求显式重定向到具有“文件扩展名”的URL。

Options -Multiviews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^/?(.+)\.html$ /$1/ [R=301]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^/?([^\.]+)/?$ /$1.html [END]

If the above rule set results in a http status 500 ("server internal error"), then chances are that you operate a very old version of the apache http server. 如果上述规则集导致http状态为500(“服务器内部错误”),则可能是您使用的Apache HTTP服务器版本非常旧。 In that case you have to use the L flag instead of the END flag and add an additional condition to the first block: 在这种情况下,您必须使用L标志而不是END标志,并在第一个块中添加一个附加条件:

RewriteEngine On

RewriteCond %{REQUEST_URI} .html$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?(.+)\.html$ /$1/ [R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^/?([^\.]+)/?$ /$1.html [L]

And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files ( .htaccess style files). 还有一个一般性提示:您应该始终喜欢将此类规则放置在http服务器(虚拟)主机配置中,而不是使用动态配置文件( .htaccess样式文件)。 Those files are notoriously error prone, hard to debug and they really slow down the server. 众所周知,这些文件容易出错,难以调试,并且确实降低了服务器的速度。 They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare). 仅当您无法控制主机配置(阅读:真正便宜的托管服务提供商)或者您的应用程序依赖于编写自己的重写规则(这显然是安全噩梦)时,才将它们作为最后一个选项来支持。 )。

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

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