我想要实现的是:
从结构来看:http + .html
结构: https + .com
以下是示例: 来自: http://www.chiamhuiy.com/2022/07/fat-boys-food-review-burger-making-celine-chiam-singapore-lifestyle-beauty-and-travel-blogger-4.html / 致: https://www.chiamhuiy.com/2022/07/fat-boys-food-review-burger-making-celine-chiam-singapore-lifestyle-beauty-and-travel-blogger-4/ 可以看到有404错误,无法实现重定向。
我已经在cloudflare上安装了SSl,并且我已经测试了从http到https .com版本,它会自动重定向。 但是,.html 无法重定向到非 html(仅限 .com 版本)
以下是我遵循并所做但未能实现我想要的:
我原来的和现有的 .htaccess 看起来像
<IfModule mod_rewrite.c>
RewriteEngine On
#In here, please use the fourth attempt codes.It will work for redirection.
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
.html 第一次尝试(失败)
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
.html 第二次尝试(失败)
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.html [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
.html 第三次尝试(失败)
RewriteRule (.+)\.html?$ http://example.com/$1/ [R=301,L]
.html 第四次尝试(失败)
RewriteRule ^/?(.*).(html)$ /$1 [R=301,L]
希望有高手可以分享一段.htaccess代码来完成它。
首先,确保您的 Apache 服务器配置中启用了 mod_rewrite。大多数服务器默认启用它。
以下是在 .htaccess 文件中设置重定向的方法:
备份您的 .htaccess 文件:在进行任何更改之前,创建现有 .htaccess 文件的备份始终是一个好习惯。
编辑您的 .htaccess 文件:如果您使用 WordPress,您可以在 WordPress 安装的根目录中找到 .htaccess 文件。使用文本编辑器编辑文件。
在 .htaccess 文件中的 WordPress 规则开头或之前添加以下代码:
在此输入代码 重写引擎开启 重写库 / RewriteRule ^(.+).html$ http://www.example.com/$1.com [L,R=301]
将 http://www.example.com/ 替换为您的实际域名。
此代码使用 mod_rewrite 将扩展名为 .html 的 URL 重定向到相同的 URL,但附加了 .com。 [R=301] 标志表示永久重定向,这对 SEO 有好处,因为它通知搜索引擎更改是永久性的。