繁体   English   中英

.htaccess:删除html扩展名,强制www和https

[英].htaccess: remove html extension, force www and https

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

index.html
page1.html
page2.html
etc

我想将.htaccess配置为:

-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?

提前致谢

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

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]

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

#OK we force https and www
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.exmple.com/$1 [L,R=301]

#  remove .html from uri
RewriteRule ^([^\.]+)$ $1.html [NC,L]

# remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# redirect index.html -> /
RewriteCond %{REQUEST_URI} \.html$
Redirect /index.html /

除了我输入example.com/page1.html以外,其他所有方法似乎都可以使用。 它不会删除html

暂无
暂无

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

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