.htaccess: remove html extension, force www and https

淺唱寂寞╮ 提交于 2019-12-19 11:23:17

问题


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

index.html
page1.html
page2.html
etc

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

How should I configure my .htaccess?

Thanks in advance


回答1:


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.




回答2:


#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 /

All seems to work except when I enter example.com/page1.html. It doesn't remove html



来源:https://stackoverflow.com/questions/43170532/htaccess-remove-html-extension-force-www-and-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!