【问题标题】:How to set redirection using regex in wordpress?如何在 wordpress 中使用正则表达式设置重定向?
【发布时间】:2021-08-28 01:36:54
【问题描述】:

我想设置重定向

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)

这不起作用:(

【问题讨论】:

    标签: regex wordpress nsregularexpression


    【解决方案1】:

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

    // eg. $url is your URL
    
    $regex = '/\/tag\/([0-9]{1,})\/t([0-9]{1,})/g';
    $replace = '/tag/$1/text/$2';
    
    $url = preg_replace($regex, $replace, $url);
    
    wp_redirect($url);
    

    【讨论】:

    • 谢谢!我不知道'$'。成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多