[英]add_rewrite_rule wordpress with w3 total cache
I am trying to rewrite rule to seo friendly. 我正在尝试将规则重写为seo友好。 Here is the code I put in my theme function.php 这是我放入主题function.php中的代码
add_action('init', 'add_my_rewrite_rule');
function add_my_rewrite_rule(){
add_rewrite_rule('^quiz/([^/]*)/([^/]*)?','index.php?pagename=quiz&quiztitle=$matches[1]&quizid=$matches[2]','top');
}
add_filter('query_vars','set_quiz_query_var');
function set_quiz_query_var($vars) {
array_push($vars, 'quiztitle');
array_push($vars, 'quizid');
return $vars;
}
Because this snippet didn't work, I also tried to rewrite .htaccess rule. 由于此代码段无效,因此我也尝试重写.htaccess规则。
RewriteRule ^quiz/([^/]+)/([^/]+)$ /index.php?pagename=quiz&quiztitle=$1&quizid=$2 [NC,R=301,L]
I guess this is because of w3 total cache. 我猜这是因为w3总缓存。 I spent whole night but couldn't figure it out alone.... 我整夜都在度过,但无法独自解决...。
刷新重写规则-转到设置永久链接,然后单击“保存”
Sorry for answering myself! 很抱歉回答自己! I spent whole night and day to find out the issue. 我花了一整夜的时间来找出问题所在。
I used add_filter instead of add_action and it worked out. 我用add_filter代替了add_action,结果解决了。
add_filter('rewrite_rules_array','add_my_rewrite_rule');
function add_my_rewrite_rule($rules)
{
$newrules = array();
$newrules['^quiz/([^/]+)/([^/]+)/?'] = 'index.php?pagename=quiz&quiztitle=$matches[1]&quizid=$matches[2]';
return $newrules + $rules;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.