I've just created a rewrite rules to integrate pagination in my page music (displaying custom post_type "music") in order to : 1. displaying a specific page from a long musical playlist. 2. in this page, playing a specified track

Here is the rule for pagination :

add_rewrite_rule('music/page/([0-9])+/([^/]+)/?', 'index.php?pagename=music&paged=$matches[1]&trackid=$matches[2]', "top");

But now when I enter : http://carbonpaper-dev.imperatorium.org/music/page/2/track-2442

Wordpress rewrite my link with a repetition of /page/2/ : http://carbonpaper-dev.imperatorium.org/music/page/2/track-2442/page/2/

I search a solution to keep the first URL. I try get_rewrite_permalink() function but it's not ok..

Any suggestions ?


The blog is in test phase so sorry for inconveniences...

3 Answers 3


I believe this problem is less to do with the rewrite rule, which on the face of it looks like it should work, and more to do with WordPress' canonical redirect.

Basically when WordPress receives an url - it interprets it as a query. It then looks again at the url to check that the url is 'correct' (or canonical). For instance it may have superfluous forward slashes and so forth.

If the url is deemed 'incorrect' - you are redirected to the 'correct' url. The idea is that you don't have multiple urls pointing the same content (they are all redirected to one canonical url).

The file that is responsible for this is /wp-includes/canonical.php. In particular the function redirect_canonical() is hooked onto the template_redirect action. The redirect_canonical() function has its own filter:

add_filter('redirect_canonical', 'wpse50912_redirect_canonical',10,2)
function wpse50912_redirect_canonical($redirect_url, $requested_url){

     //Check the requested url is an url for your (paginated) music archive
     //Return the url you want in the address bar.

     return $redirect_url;

You can disable canonicalisation - by unhooking the redirect_canonical function from the template_redirect hook. Or can modify its behaviour using the redirect_canonical. Either way I would recommend disabling temporarily in order to verify this is the cause.


hard to tell without looking closer at your code, but I have a sneaky suspicion it might somehow relate to the fact that you use the page keyword. Perhaps somewhere else there's a rule that looks for it.

A couple of quick tests perhaps worth trying:

  1. change 'music/page/([0-9])... to 'music/pg/([0-9])... (or anything other than the word page)
  2. Perhaps try using bottom instead of top to get your rule processed after the wordpress stuff

I did a lot of research about changing the page variable or tried to remove it. I learned that Wordpress works only with the page notation at the end of the URL. You can change this behavior but than it's required to change code inside the Wordpress core files. Your question is not exact the same what I tried, but it's about the same "problem". I'm sorry but I you need to use the URL created by Wordpress.

BTW. why do you need a different URL structure?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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