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 ?
beR-
The blog is in test phase so sorry for inconveniences…
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 thetemplate_redirect
action. Theredirect_canonical()
function has its own filter:You can disable canonicalisation – by unhooking the
redirect_canonical
function from thetemplate_redirect
hook. Or can modify its behaviour using theredirect_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:
'music/page/([0-9])...
to'music/pg/([0-9])...
(or anything other than the wordpage
)bottom
instead oftop
to get your rule processed after the wordpress stuffI 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?