.htaccess:删除html扩展名,强制www和https [英] .htaccess: remove html extension, force www and https

查看:87
本文介绍了.htaccess:删除html扩展名,强制www和https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下页面开发了一个简单而纯正的html网站:

I developed a simple and pure html site with this pages:

index.html
page1.html
page2.html
etc

我想将.htaccess配置为:

And I would like to configure .htaccess to:

-Force https
-Force www
-Remove .html extension (/page1.html -> /page1)
-Redirect index.html -> /
-When someone types /page1.html to be redirected to /page1 (without html) or (if not possible) to 404 error page

我应该如何配置.htaccess?

How should I configure my .htaccess?

预先感谢

推荐答案

要删除.html并强制 https://www ,您可以使用以下规则:

To remove .html and to force https://www , you can use the following rule :

RewriteEngine on
#force https+www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule (.*) https://www.%1/$1 [NE,L,R]
#Remove .html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ /$1.html [L]

在测试这些规则之前,请清除浏览器缓存.

Clear your browser cache before testing these rules.

这篇关于.htaccess:删除html扩展名,强制www和https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆