动态Htaccess重定向-Wordpress

问题描述 投票:0回答:1

我正在寻找一种在Wordpress中执行批量301重定向的解决方案。我查看了一些常见的htaccess重定向,但找不到适合我所需的正确解决方案。

当前网址格式:

website.com/year/month/post-name (e.g website.com/2020/02/post-name)

我想实现的格式:

website.com/blog/post-name

后缀将始终保持不变,它只是网址的第一部分需要调整。

任何人都可以帮忙吗?

php wordpress .htaccess
1个回答
1
投票

您可以在RewriteCondition中将请求URI的格式(如year/month/post-name与正则表达式进行匹配,如果请求URI匹配,则具有重写规则。

Snippet:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/?\d+\/\d+\/.+ [NC]
RewriteRule ^\d+\/\d+\/(.+) http://website.com/blog/$1 [NC,L,QSA]

Demo: https://htaccess.madewithlove.be?share=f2c421fe-e23d-50a9-8988-9a7bac647951

在上面的规则中,我们执行不区分大小写的匹配,并获取正则表达式组1中的post-name部分,即(.+)。现在,我们在重定向URL中添加$1,其中1是组号(因为组号0是整个正则表达式本身)。

更新:

您可以添加状态码为R 302if this is a temporary redirectpermanent redirect 301重定向信号(也将在SEO搜寻引擎中更新)。

为此,您可以从]更改重写规则>

301

to

RewriteRule ^\d+\/\d+\/(.+) http://website.com/blog/$1 [NC,L,QSA]
© www.soinside.com 2019 - 2024. All rights reserved.