重写.htaccess的规则

时间:2019-02-27 17:16:17

标签: html .htaccess webserver frontend

我对.htaccess文件有一个基本问题。

目前,我的包括:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

将所有链接重定向到网站的超文本传输​​协议安全(https)。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^\.]+)$ $1.html [L]

重写所有.html文件(不带文件扩展名)。

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/?(.+)/?$ /$1/index.html [L]
RewriteRule ^/?(.+/)index\.html$ /$1 [R=301]

从网站网址中删除index.html。 由于第二条规则,我是否应该删除文件扩展名(.html)?

ErrorDocument 404 /404.html

不言自明。 我也应该在这里删除文件扩展名吗?

总体而言-代码是否正确,这是我的.htaccess文件中的样子吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我最终使用了以下代码:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^\.]+)$ $1.html [L]

ErrorDocument 404 /404.html

index.html从URL删除规则没有用,因为我更改了html部分中的链接:

<a href="index.html">Homepage</a>

收件人:

<a href="/">Homepage</a>

和类比:

<a href="en/index.html">English Homepage</a>

收件人:

<a href="en">English Homepage</a>