【问题标题】:.htaccess URL redirect old link format to new.htaccess URL 将旧的链接格式重定向到新的
【发布时间】:2018-10-11 00:58:27
【问题描述】:

最近我在我的自托管 wordpress 博客上将链接格式更改为文章,我希望旧书签能够无缝重定向到新链接,这样读者就不会找不到页面。

旧格式为:https://domain.tld/index.php/yyyy/mm/dd/title-of-post

新格式为:https://domain.tld/title-of-post/

我当然做了这个改变,因为它更好,而且时间并不是我博客的真正因素。我在.htaccess 文件中使用了Apache 的RewriteRule 指令,但我无法让模式匹配。这是.htaccess 文件的当前状态:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php/[0-9]+/[0-9]+/[0-9]/.*?$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

但之前我尝试过这样的模式:

^index\.php/?([-a-zA-Z0-9_+]+)/?([-a-zA-Z0-9_+]+)/?([-a-zA-Z0-9_+]+)/.*?$

和

^index\.php/2018/03/30/.*?$

甚至

(index)(\\.)(php)(\\/)(\\d)(\\d)(\\d)(\\d)(\\/)(\\d)(\\d)(\\/)(\\d)(\\d)(\\/)((?:[a-z][a-z]+))

其中我最后一次尝试使用txt2re

在匹配我提供的 URL 时,这些都没有给我带来任何运气。我希望旧链接重定向到新的相应链接。

【问题讨论】:

  • @SufiyanGhori 没错
  • 您是否尝试过在 wordpress 管理面板中修改永久链接设置? wordpress 应该处理这种重定向
  • @SufiyanGhori 我有,但无济于事。 wordpress 删除的唯一内容是 index.php。我什至从 htaccess 文件中删除了规则,它仍然只是删除了 index.php
  • 请看最新回答,我已经添加了重定向规则

标签: regex wordpress apache .htaccess


【解决方案1】:

与其修改您的.htaccess,不如使用以下重定向插件,

https://wordpress.org/plugins/redirection/

它将为您处理所有重定向

这对我和其他许多人都有效。请让我知道这是否适合您。

编辑:

对于您的具体情况,以下规则将起作用

RedirectMatch 301 ^/index\.php/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://yourdomain.com/$4

您可以在此处阅读有关此规则的更多信息,

https://perishablepress.com/redirect-wordpress-date-archives-htaccess/

【讨论】:

  • 谢谢!天哪,我一直在为此绞尽脑汁。重定向插件就像一个魅力
  • 干杯!很高兴我帮助了
猜你喜欢
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 2022-06-11
  • 2015-09-14
  • 1970-01-01
  • 2016-02-11
  • 2015-07-16
  • 1970-01-01
相关资源
最近更新 更多