如何在wordpress中使用正则表达式设置重定向?

How to set redirection using regex in wordpress?

我想设置重定向

https://test.com/tag/1/t3 (source url) → https://test.com/tag/1/text/3 (target url)

https://test.com/tag/2/t4 (source url) → https://test.com/tag/2/text/4 (target url)

我当前的设置是

https://test.com/tag/([0-9]{1,})/t?([0-9]{1,}) (source url)

↓

https://test.com/tag/([0-9]{1,})/text/([0-9]{1,}) (target url)

这行不通:(

我还没有实际测试过,但我认为下面的一段代码可能会解决问题。

// eg. $url is your URL

$regex = '/\/tag\/([0-9]{1,})\/t([0-9]{1,})/g';
$replace = '/tag//text/';

$url = preg_replace($regex, $replace, $url);

wp_redirect($url);