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 !

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.com/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

| improve this answer | |
  • Ok, I understand. I find this : add_filter('redirect_canonical','my_redirects',10,2); code function my_redirects($redirect_url, $requested_url) { // do stuff return $redirect_url; } code But I don't know how can I write this. I tried to change .htaccess too. It is a wrong way too ? – nicocube Sep 28 '15 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.