最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rewrite rules - add_rewrite_rule doesn't work for me

programmeradmin0浏览0评论

I'm trying to use add_rewrite_rule to make custom URL. My problem is the same as here : Custom Rewrite with Query vars

But I tried the 2 solutions, and it doesn't work for me.

For testing, I installed locally a new version of wordpress with default theme Twenty fifteen. I installed the plugin Rewrite rules Inspector to see all the active rules. I added a page "test" and added this function in function.php :

add_filter( 'query_vars', 'query_vars' );
function query_vars( $vars )
{
    array_push($vars, 'information');
    return $vars;
}

add_action( 'rewrite_rules_array', 'rewrite_rules' );
function rewrite_rules( $rules )
{
    $newrules = array();
    $newrules[ 'test/information/people/?$' ] = 'index.php?pagename=test&information=people';

    return $newrules + $rules;
}

I don't forget to flush the new rules.

But when I go to : index.php?pagename=test&information=people

wordpress write the url : ?pagename=test&information=people

and not : test/information/people/

Why ? What's wrong ? It seems that Wordpress has a rule over mine. I don't understand ...Help !

I'm trying to use add_rewrite_rule to make custom URL. My problem is the same as here : Custom Rewrite with Query vars

But I tried the 2 solutions, and it doesn't work for me.

For testing, I installed locally a new version of wordpress with default theme Twenty fifteen. I installed the plugin Rewrite rules Inspector to see all the active rules. I added a page "test" and added this function in function.php :

add_filter( 'query_vars', 'query_vars' );
function query_vars( $vars )
{
    array_push($vars, 'information');
    return $vars;
}

add_action( 'rewrite_rules_array', 'rewrite_rules' );
function rewrite_rules( $rules )
{
    $newrules = array();
    $newrules[ 'test/information/people/?$' ] = 'index.php?pagename=test&information=people';

    return $newrules + $rules;
}

I don't forget to flush the new rules.

But when I go to : index.php?pagename=test&information=people

wordpress write the url : ?pagename=test&information=people

and not : test/information/people/

Why ? What's wrong ? It seems that Wordpress has a rule over mine. I don't understand ...Help !

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Sep 28, 2015 at 13:55 nicocubenicocube 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You've got it the wrong way round - WordPress won't rewrite (redirect) your query string (known as canonicalization), but it will pass your parameters through if you use the rewritten URL:

http://example/test/information/people

Now if you print_r( $wp-query_vars ) in your template, you should get something like:

Array
(
    [pagename] => test
    [information] => people
)

To redirect params to the "pretty" (rewritten) URL, take a look at redirect_canonical

发布评论

评论列表(0)

  1. 暂无评论