通过正则表达式重定向Wordpress中的自定义分类

时间:2011-03-10 12:20:27

标签: php regex

我正在尝试将自定义查询与正则表达式匹配,以便我可以重定向此:

http://www.mysite.com/category/2010/11/title-of-post/?tags=titleoftag

到此:

http://www.mysite.com/category/?tags=titleoftag

我尝试过像这样重定向:

来源网址:http://www.mysite.com/category/(.*)/?tags=(.*)

目标网址:http://www.mysite.com/category/?tags=$2

但是这也与目标URL本身匹配,因此重定向会陷入循环中。

我很感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

这段代码对我有用:(正则表达式的分隔符是#,但你可以很容易地改变它)

$subject = 'http://www.mysite.com/category/2010/11/title-of-post/?tags=titleoftag';
$subject2 = 'http://www.mysite.com/category/?tags=titleoftag';

$regex = '#http://www.mysite.com/category/(.*)/\?tags=(.*)#i';

$replace = 'http://www.mysite.com/category/?tags=$2';

var_dump(preg_replace($regex, $replace, $subject)); // replaces the text correctly


$result = preg_match($regex, $subject2);
var_dump($result); // finds 0 results