Apache .htaccess:删除html扩展名,强制使用www和https
我用以下页面开发了一个简单而纯粹的html站点:Apache .htaccess:删除html扩展名,强制使用www和https,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我用以下页面开发了一个简单而纯粹的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
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
提前谢谢
#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要删除.html并强制,您可以使用以下规则:
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]
在测试这些规则之前,请清除浏览器缓存。所有这些问题都在这里被回答了数百次,甚至数千次。所有这些答案都没有帮助你开始?告诉我们:为什么所有这些答案都不能让你继续,为什么一个额外的答案会起作用?是的,我可以在这里找到如何强制使用https和www。但是我找不到如何在浏览器中键入page.html时删除“html”扩展名。然后我建议你从这两个方面开始,然后发布到目前为止您拥有的动态配置文件,并解释为什么您试图解决剩余的问题不起作用。对于这个问题也有无穷无尽的答案,所以假设你环顾四周,你的尝试肯定会有一些问题。但是在你发布你的代码之前,我们无法帮助你,抱歉。工作正常+1和+1啤酒!!太高兴了!