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

时间:2017-04-02 16:11:40

标签: 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 html) or (if not possible) to 404 error page

我应该如何配置我的.htaccess?

提前致谢

2 个答案:

答案 0 :(得分:2)

要删除.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]

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

答案 1 :(得分:0)

#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