Discussion:
Custom URL Rewrite Help
Md Mahmudur Rahman
2013-07-02 09:17:13 UTC
Permalink
Dear Hackers,

I have a custom post type (artist), which has a custom URL rewrite for a
custom page template as given below:

*URL:* http://localhost/artist/artist-name/videos/3/

*Code for URL Rewrite:*

add_action( 'init', 'my_artist_rewrite_rule' );

function my_artist_rewrite_rule() {

add_rewrite_rule( '([^/]+)/videos(/[0-9]+)?/?$',
'index.php?post_type=artist&name=$matches[1]&videos=yes&page=$matches[2]',
'top' );


}

And I am manually flushing the permalink settings each time I am making any
change to the rewrite rule to reflect the changes. But it seems that the
custom rewrite rule is not working. Here is the response of the $wp object:

[query_vars] => Array
(
[name] => artist-name
[page] => /3
[news] => yes
[post_type] => artist
)

[query_string] => name=artist-name&page=%2F3&news=yes&post_type=artist
[request] => artist/artist-name/3
[matched_rule] => ([^/]+)(/[0-9]+)?/?$
[matched_query] => name=artist-name&page=%2F3
[did_permalink] => 1

So the requesting URL is always matching to the below rule ignoring the
custom rewrite rule:

[matched_rule] => ([^/]+)(/[0-9]+)?/?$

I have checked the custom rule with a rewrite rules inspector plugin and
requested URL always matched with the custom URL rewrite rule but that is
not the case in real.

Not sure what is happening behind? Hope someone can give me a clue.

*Many thanks,*

*Mahmudur Rahman*

<http://www.79mplus.com/>
Dobri
2013-07-02 12:41:52 UTC
Permalink
WP complies with the first rule matched. add_rewrite_rule adds your rule to the bottom of the list (I think). Thus, it's never reached. You have a better shot doing this with something like this: (straight out of my code so ignore variable names and stuff.)

function add_rewrite_rules($aRules) {
global $press_releases_slug;
$year = date('Y');
$aNewRules = array(
$press_releases_slug.'/([0-9]{4})/?$' => 'index.php?post_type='.$press_releases_slug.'&year=$matches[1]&posts_per_page=-1&nopaging=1&orderby=post_date&order=DESC',
$press_releases_slug.'/search/(.+?)/?$' => 'index.php?s=$matches[1]&post_type='.$press_releases_slug.'&orderby=post_date&order=DESC',
$press_releases_slug.'/?$' => 'index.php?post_type='.$press_releases_slug.'&year='.$year.'&posts_per_page=-1&nopaging=1&orderby=post_date&order=DESC'
);
$aRules = $aNewRules + $aRules;
return $aRules;
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');

As you can see, I create an array of my rules and prepend it to the existing ones. Presto, they become the first ones that match. Not my best piece of work but it is functional. I think add_rewrite_rule is more about adding something that doesn't conflict with the stuff wp builds by default. I guess another way would be when creating your post type to set rewrite to false. (http://codex.wordpress.org/Function_Reference/register_post_type) Whichever works for you. Enjoy!

~Dobri
Post by Md Mahmudur Rahman
Dear Hackers,
I have a custom post type (artist), which has a custom URL rewrite for a
*URL:* http://localhost/artist/artist-name/videos/3/
*Code for URL Rewrite:*
add_action( 'init', 'my_artist_rewrite_rule' );
function my_artist_rewrite_rule() {
add_rewrite_rule( '([^/]+)/videos(/[0-9]+)?/?$',
'index.php?post_type=artist&name=$matches[1]&videos=yes&page=$matches[2]',
'top' );
}
And I am manually flushing the permalink settings each time I am making any
change to the rewrite rule to reflect the changes. But it seems that the
[query_vars] => Array
(
[name] => artist-name
[page] => /3
[news] => yes
[post_type] => artist
)
[query_string] => name=artist-name&page=%2F3&news=yes&post_type=artist
[request] => artist/artist-name/3
[matched_rule] => ([^/]+)(/[0-9]+)?/?$
[matched_query] => name=artist-name&page=%2F3
[did_permalink] => 1
So the requesting URL is always matching to the below rule ignoring the
[matched_rule] => ([^/]+)(/[0-9]+)?/?$
I have checked the custom rule with a rewrite rules inspector plugin and
requested URL always matched with the custom URL rewrite rule but that is
not the case in real.
Not sure what is happening behind? Hope someone can give me a clue.
*Many thanks,*
*Mahmudur Rahman*
<http://www.79mplus.com/>
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Ryan McCue
2013-07-02 12:45:28 UTC
Permalink
Post by Dobri
WP complies with the first rule matched. add_rewrite_rule adds your rule to the bottom of the list (I think).
Note the 'top' parameter there.

I believe the reason it's not matching is because the regex is wrong.

([^/]+)/videos(/[0-9]+)?/?$

The first part of that matches anything but a slash, however the URL is:

artist/artist-name/videos/3/

So, I don't believe that will match correctly. Try (.+) instead for the
first part.
--
Ryan McCue
<http://ryanmccue.info/>
Loading...