htaccess - HTTP到HTTPS和.HTML到/

问题描述:

我需要 .htaccess 的帮助:

强制 http:/ / https://

AND

强制 .html /

到目前为止我所拥有的:

What I have so far:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]


Options -MultiViews

RewriteEngine On
#1 This line checks if the https is off
RewriteCond %{HTTPS} ^off$
#then, redirect to https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [NC,L,R]
#2 this line checks if the request is /file.html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
#then redirect /file.html to /file
RewriteRule ^ /%1 [NC,L,R]

#3 if the request is not for dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is an existing filename
RewriteCond %{REQUEST_FILENAME}.html -f
#then rewrite /file to /file.html
RewriteRule ^([^/]+)/?$ $1.html [NC,L]

在上面的例子中,第一个条件当原始方案是HTTP时满足,然后处理规则。 HTTP转到HTTPS。第一轮重写处理在这里结束。

In the example above, the first condition is met when the original scheme is HTTP, and then the rule is processed. HTTP goes to HTTPS. The first Round of rewrite processing ends here.

在第二轮中,mod_rewrite接受URI /file.html并且规则将其重定向到/ file,因为/ file目录中不存在,因此我们需要将其重写为原始文件#3 ...

In the second round, mod_rewrite accepts the URI /file.html and the rule redirects it to /file, since the /file does not exist in directory, so we need to rewrite it to the original file #3...