动态Htaccess重定向-Wordpress

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

当前网址格式:

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

我想实现的格式:

website.com/blog/post-name

post slug始终保持不变,只是url的第一部分需要调整。

有人可以帮忙吗?

评论
  • 碧鲁翠梅
    碧鲁翠梅 回复

    You could the match the format of the request URI like year/month/post-name with regex in RewriteCondition and have a rewrite rule for it if the request URI matches.

    片段:

    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

    In the above rule, we perform a case insensitive match and grab the post-name part in group 1 of regex which is (.+). Now, we add that in our redirection URL with a $1 where the 1 is the group number(as group number 0 is the entire regex itself).