Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating some custom rewrites using the code below. Everything works fine, as far as urls such as this:

example.com/reviews/showname

Tells my theme to use the special archive page and passes the parameters I need. The problem I run into is with pagination. Wordpress generates the links properly:

example.com/reviews/showname/page/2/

but the result is a page not found. My first thought was to add a second rewrite:

 'reviews/(.+)/(.+)/(.+)' =>  'index.php?reviews=1&showarchive=1&category_name=' . $wp_rewrite->preg_index(1) . '&pgd=' . $wp_rewrite->preg_index(2) . '&pgn=' . $wp_rewrite->preg_index(3),

So that I could capture the 'page' and the page number and use those to code an offset into the query and set the paged query var, but I find when I add this it cancels out the other so that:

example.com/reviews/showname

Now returns page not found but

example.com/reviews/showname/page/1/

Works. For SEO purposes I'd like to keep the

example.com/reviews/showname

Style url without /page/1 — is there any way to get the rewrites working for both conditions?

add_filter('query_vars', 'eparchive_queryvars' );

function eparchive_queryvars( $qvars )
{
  $qvars[] = 'showarchive';
  $qvars[] = 'reviews';
  $qvars[] = 'pgd';
  $qvars[] = 'pgn';
  return $qvars;
}    

add_action('generate_rewrite_rules', 'custom_add_rewrite_rules');

    function custom_add_rewrite_rules( $wp_rewrite ) 
    {
      $new_rules = array( 

         'reviews/(.+)' =>  'index.php?reviews=1&showarchive=1&category_name=' . $wp_rewrite->preg_index(1)
         );
      $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You can try these rules:

reviews/([^/]+)/page/?([0-9]{1,})/?$
reviews/([^/]+)/?$

It's informative to check out the active rewrite rules with:

function show_rewrite_rules( $rules ) {
    if(is_admin()) echo "<pre>".print_r($rules,true)."</pre>";
    return $rules;
}
add_filter('rewrite_rules_array','show_rewrite_rules');

and then visit /wp-admin/options-permalink.php.

There exists also some good plugins for analysing the rewrite rules, for example the Monkeyman Rewrite Analyzer.

share|improve this answer
    
Thanks Birgire! I guess it was a matter of using the right example. –  Jon Lachonis Apr 10 '13 at 15:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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