Wordpress重写匹配不返回正确的值

时间:2016-04-28 09:07:09

标签: wordpress mod-rewrite

我创建了一个规则,用于将网址croziere/{example}/{example}重写为以最后两个斜杠内容的编号命名的页面。例如croziere/destination/ukindex.php?pagename=destination-uk

 function custom_rewrite_rule() {

    add_rewrite_rule('^croaziere/([^/]*)/([^/]*)/?','/index.php?  page_name=$matches[1]-$matches[2]','top');

 }


 add_action('init', 'custom_rewrite_rule', 10, 0);

这里是htaccess的样子:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /travel/
RewriteRule ^index\.php$ - [L]
RewriteRule ^croaziere/([^/]*)/([^/]*)/? /travel/index.php?pagename=$matches[1]-$matches[2] [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /travel/index.php [L]
</IfModule>

这是重写调试:

1 个答案:

答案 0 :(得分:0)

正确的正则表达式是:

croziere/([a-z]*)/([a-z]*)

croziere/destination/uk将返回

array(
   0    =>  croziere/destination/uk
   1    =>  destination
   2    =>  uk
)

更新,如果你想将匹配组合成一个单词....确认工作假设你有一个结构something-something的页面名称并且页面存在。 btw确保刷新重写规则而不是直接添加到htaccess,如果你需要正确地转义相应的字符。

function custom_rewrite_rule() {

   add_rewrite_rule('croaziere/([a-z]*)/([a-z]*)','/index.php?pagename=$1-$2','top');

 }


 add_action('init', 'custom_rewrite_rule');