将文件名中的 .html 转换为 -html

Convert .html to -html in file name

我将我的网站转换为 WordPress。在此过程中,为了满足 WordPress 关于永久链接格式的规则,我不得不将文件名从以 .html 结尾更改为以 -html.

结尾

不幸的是,有来自其他站点的反向链接指向以 .html 结尾的旧页面。因此,当有人单击这些反向链接之一时,无法在新站点上找到该页面。

因此,我想使用 .htaccess 永久重定向对以 .html 结尾的 URI 的所有请求,以便它们以 -html.

结尾

例如:

https://example.com/file1.html

需要永久重定向到

https://example.com/file1-html

遗憾的是,我的代码产生了 500 个错误。

以下是我根据在 Apache 手册中找到的内容编写的内容:

<Directory /home/accountname/public_html>
    RewriteEngine on
    RewriteBase /home/accountname/public_html

    RewriteCond .html !-f
    RewriteRule ^(.*).html$ -html [R=301,L]
</Directory>

有人可以帮忙吗?

<Directory> .htaccess 中不允许指令。

将此规则放在您的主 WP .htaccess 中:

RewriteEngine on

RewriteRule ^(.+?)\.html$ /-html [R=301,L,NE,NC]

确保这是 RewriteEngine on 行下方的第一条规则。