Wordpress重写网址不适用于自定义模板

时间:2017-09-04 07:11:04

标签: wordpress url-rewriting

网址为:

mywebsite.com/custom/?var1=random_text

我想要

mywebsite.com/custom/random_text

这是我的functions.php文件中的代码:

add_filter( 'query_vars', 'wpse12965_query_vars' );
function wpse12965_query_vars( $query_vars )
{
    $query_vars[] = 'var1';
    return $query_vars;
}

add_action( 'init', 'wpse12065_init' );
function wpse12065_init()
{
    add_rewrite_rule(
        'custom(/([^/]+))?/?',
        'index.php?pagename=custom&var1=$matches[1]',
        'top'
    );
}

但它仍然会返回404错误。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的代码看起来很完美,尝试添加flush_rewrite_rules函数。

add_filter( 'query_vars', 'wpse12965_query_vars' );
function wpse12965_query_vars( $query_vars )
{
 $query_vars[] = 'var1';
 return $query_vars;
}
add_action( 'init', 'wpse12065_init' );
function wpse12065_init(){
  add_rewrite_rule(
    'custom(/([^/]+))?/?',
    'index.php?pagename=custom&var1=$matches[1]',
    'top'
 );
flush_rewrite_rules();
}