.html 页面的 301 重定向

301 Redirect for .html pages

在搜索 Whosebug 并尝试多种 Regex 方法后,我似乎无法获得将生成每个示例的重定向。com/example.html 网站上的页面改为指向简单的示例。 com/example。我的 .htaccess 文件如下所示:

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

我的第一次尝试是

RedirectMatch 301 (.*)\.html$ https://example.com

但是虽然它适用于单个 .html 页面,但它导致主页重定向到 https://example.com/index,这是一个 404.

我最近的尝试是在底部添加这个

RewriteRule ^(.*)\.html$ / [L,R=301]

那根本行不通。然后我把同一行放在行

之前

RewriteRule . /index.php [L]

所以块看起来像这样

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ / [L,R=301]
RewriteRule . /index.php [L]

但这只会导致网站加载非常缓慢,并且在加载图像和样式表时会损坏。

有什么我不明白的方法吗?

您可以在现有规则之前使用此重定向规则:

RewriteEngine On

# removes .htm, .html, index.htm or index.html - case ignore
RewriteCond %{REQUEST_URI} ^/(.*/)?(?:index|([^/]+))\.html?$ [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]