0

I'm building a robust filtering system for my WordPress site, and I really want to have pretty permalinks for the resulting URLs. But because I'm offering a large amount of different filtering options, the different rules required for rewriting are getting out of control. I'm currently running at 80 rewrite rules, with no idea of knowing if this is going to badly impact my site's performance (though I guess the real answer to that question is "it depends").

The problem is that every time I add a new filter choice, it doubles the amount of rules unless I can catch it in a regular expression. How can I simplify rewrite rules with these requirements?

  • 3 taxonomies can be included in any combination and any order, but must always come before any of the below mention additional URL segments. URLs without any of these taxonomies are also allowed. Taxonomies are genre, artist and the built-in tag. URL segments looks like
    /genre/neurofunk/tag/bassdrive-xposure-show/artist/ben-xo+dj-liquid/;
  • To show posts within a specific date range, two URL segments can be included to set a date range WHERE clause, like /20120826/20140513/;
  • Ordering by built-in columns and custom meta_values is possible by using the /by/ URL segment, followed by the key. To be able to distinct between built-in and custom keys, a prefixed underscore is used: /by/comment_count/ vs. /by/_downloads/;
  • Since default ordering is DESC, I include /asc/ as URL segment when I want to sort that way;
  • To ensure working pagination, I duplicated each rule to include a /page/ segment.

My rewrite rules

function tjnz_rewrite( $wp_rewrite ) {
    $rules = array(
        // with daterange + 3 params + orderby meta
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/asc\/page\/([0-9])\/?$' => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8) . '&mixmeta=' . $wp_rewrite->preg_index(9) . '&order=asc&paged=' . $wp_rewrite->preg_index(10),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/asc\/?$'                => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8) . '&mixmeta=' . $wp_rewrite->preg_index(9) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/page\/([0-9])\/?$'      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8) . '&mixmeta=' . $wp_rewrite->preg_index(9) . '&paged=' . $wp_rewrite->preg_index(10),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/?$'                     => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8) . '&mixmeta=' . $wp_rewrite->preg_index(9),
        // with daterange + 3 params + orderby native
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/asc\/page\/([0-9])\/?$'  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8) . '&orderby=' . $wp_rewrite->preg_index(9) . '&order=asc&paged=' . $wp_rewrite->preg_index(10),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/asc\/?$'                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8) . '&orderby=' . $wp_rewrite->preg_index(9) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/page\/([0-9])\/?$'       => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8) . '&orderby=' . $wp_rewrite->preg_index(9) . '&paged=' . $wp_rewrite->preg_index(10),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/?$'                      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8) . '&orderby=' . $wp_rewrite->preg_index(9),
        // with daterange + 3 params w/o orderby
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/page\/([0-9])\/?$'                  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8) . '&paged=' . $wp_rewrite->preg_index(9),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/?$'                                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&datelo=' . $wp_rewrite->preg_index(7) . '&datehi=' . $wp_rewrite->preg_index(8),

        // no daterange + 3 params + orderby meta
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/asc\/page\/([0-9])\/?$' => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&mixmeta=' . $wp_rewrite->preg_index(7) . '&order=asc&paged=' . $wp_rewrite->preg_index(8),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/asc\/?$'                => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&mixmeta=' . $wp_rewrite->preg_index(7) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/page\/([0-9])\/?$'      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&mixmeta=' . $wp_rewrite->preg_index(7) . '&paged=' . $wp_rewrite->preg_index(8),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/?$'                     => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&mixmeta=' . $wp_rewrite->preg_index(7),
        // no daterange + 3 params + orderby native
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/asc\/page\/([0-9])\/?$'  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&orderby=' . $wp_rewrite->preg_index(7) . '&order=asc&paged=' . $wp_rewrite->preg_index(8),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/asc\/?$'                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&orderby=' . $wp_rewrite->preg_index(7) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/page\/([0-9])\/?$'       => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&orderby=' . $wp_rewrite->preg_index(7) . '&paged=' . $wp_rewrite->preg_index(8),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/?$'                      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&orderby=' . $wp_rewrite->preg_index(7),
        // no daterange + 3 params w/o orderby
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/page\/([0-9])\/?$'                  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6) . '&paged=' . $wp_rewrite->preg_index(7),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/?$'                                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&' . $wp_rewrite->preg_index(5) . '=' . $wp_rewrite->preg_index(6),

        // with daterange + 2 params + orderby meta
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/asc\/page\/([0-9])\/?$' => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6) . '&mixmeta=' . $wp_rewrite->preg_index(7) . '&order=asc&paged=' . $wp_rewrite->preg_index(8),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/asc\/?$'                => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6) . '&mixmeta=' . $wp_rewrite->preg_index(7) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/page\/([0-9])\/?$'      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6) . '&mixmeta=' . $wp_rewrite->preg_index(7) . '&paged=' . $wp_rewrite->preg_index(8),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/?$'                     => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6) . '&mixmeta=' . $wp_rewrite->preg_index(7),
        // with daterange + 2 params + orderby native
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/asc\/page\/([0-9])\/?$'  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6) . '&orderby=' . $wp_rewrite->preg_index(7) . '&order=asc&paged=' . $wp_rewrite->preg_index(8),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/asc\/?$'                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6) . '&orderby=' . $wp_rewrite->preg_index(7) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/page\/([0-9])\/?$'       => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6) . '&orderby=' . $wp_rewrite->preg_index(7) . '&paged=' . $wp_rewrite->preg_index(8),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/?$'                      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6) . '&orderby=' . $wp_rewrite->preg_index(7),
        // with daterange + 2 params w/o orderby
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/page\/([0-9])\/?$'                  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(5) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6) . '&paged=' . $wp_rewrite->preg_index(7),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/?$'                                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(5) . '&datelo=' . $wp_rewrite->preg_index(5) . '&datehi=' . $wp_rewrite->preg_index(6),

        // no daterange + 2 params + orderby meta
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/asc\/page\/([0-9])\/?$' => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&mixmeta=' . $wp_rewrite->preg_index(5) . '&order=asc&paged=' . $wp_rewrite->preg_index(6),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/asc\/?$'                => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&mixmeta=' . $wp_rewrite->preg_index(5) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/page\/([0-9])\/?$'      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&mixmeta=' . $wp_rewrite->preg_index(5) . '&paged=' . $wp_rewrite->preg_index(6),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/?$'                     => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&mixmeta=' . $wp_rewrite->preg_index(5),
        // no daterange + 2 params + orderby native
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/asc\/page\/([0-9])\/?$'  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&orderby=' . $wp_rewrite->preg_index(5) . '&order=asc&paged=' . $wp_rewrite->preg_index(6),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/asc\/?$'                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&orderby=' . $wp_rewrite->preg_index(5) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/page\/([0-9])\/?$'       => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&orderby=' . $wp_rewrite->preg_index(5) . '&paged=' . $wp_rewrite->preg_index(6),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/?$'                      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4) . '&orderby=' . $wp_rewrite->preg_index(5),
        // no daterange + 2 params w/o orderby
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/page\/([0-9])\/?$'                  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(5) . '&paged=' . $wp_rewrite->preg_index(6),
        'filter\/(genre|tag|artist)\/(.+?)\/(genre|tag|artist)\/(.+?)\/?$'                                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(5),

        // with daterange + 1 param + orderby meta
        'filter\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/asc\/page\/([0-9])\/?$' => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4) . '&mixmeta=' . $wp_rewrite->preg_index(5) . '&order=asc&paged=' . $wp_rewrite->preg_index(6),
        'filter\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/asc\/?$'                => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4) . '&mixmeta=' . $wp_rewrite->preg_index(5) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/page\/([0-9])\/?$'      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4) . '&mixmeta=' . $wp_rewrite->preg_index(5) . '&paged=' . $wp_rewrite->preg_index(6),
        'filter\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/?$'                     => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4) . '&mixmeta=' . $wp_rewrite->preg_index(5),
        // with daterange + 1 param + orderby native
        'filter\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/asc\/page\/([0-9])\/?$'  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4) . '&orderby=' . $wp_rewrite->preg_index(5) . '&order=asc&paged=' . $wp_rewrite->preg_index(6),
        'filter\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/asc\/?$'                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4) . '&orderby=' . $wp_rewrite->preg_index(5) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/page\/([0-9])\/?$'       => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4) . '&orderby=' . $wp_rewrite->preg_index(5) . '&paged=' . $wp_rewrite->preg_index(6),
        'filter\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/?$'                      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4) . '&orderby=' . $wp_rewrite->preg_index(5),
        // with daterange + 1 param w/o orderby
        'filter\/(genre|tag|artist)\/(.+?)\/([0-9]{8})\/([0-9]{8})\/page\/([0-9])\/?$'                  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4) . '&paged=' . $wp_rewrite->preg_index(5),
        'filter\/(genre|tag|artist)\/(.+)\/([0-9]{8})\/([0-9]{8})\/?$'                                  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&datelo=' . $wp_rewrite->preg_index(3) . '&datehi=' . $wp_rewrite->preg_index(4),

        // no daterange + 1 param + orderby meta
        'filter\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/asc\/page\/([0-9])\/?$' => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&mixmeta=' . $wp_rewrite->preg_index(3) . '&order=asc&paged=' . $wp_rewrite->preg_index(4),
        'filter\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/asc\/?$'                => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&mixmeta=' . $wp_rewrite->preg_index(3) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/page\/([0-9])\/?$'      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&mixmeta=' . $wp_rewrite->preg_index(3) . '&paged=' . $wp_rewrite->preg_index(4),
        'filter\/(genre|tag|artist)\/(.+?)\/by\/(_.+?)\/?$'                     => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&mixmeta=' . $wp_rewrite->preg_index(3),
        // no daterange + 1 param + orderby native
        'filter\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/asc\/page\/([0-9])\/?$'  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&orderby=' . $wp_rewrite->preg_index(3) . '&order=asc&paged=' . $wp_rewrite->preg_index(4),
        'filter\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/asc\/?$'                 => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&orderby=' . $wp_rewrite->preg_index(3) . '&order=asc',
        'filter\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/page\/([0-9])\/?$'       => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&orderby=' . $wp_rewrite->preg_index(3) . '&paged=' . $wp_rewrite->preg_index(4),
        'filter\/(genre|tag|artist)\/(.+?)\/by\/(.+?)\/?$'                      => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&orderby=' . $wp_rewrite->preg_index(3),
        // no daterange + 1 param w/o orderby
        'filter\/(genre|tag|artist)\/(.+?)\/page\/([0-9])\/?$'                  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&paged=' . $wp_rewrite->preg_index(3),
        'filter\/(genre|tag|artist)\/(.+)\/?$'                                  => 'index.php?' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2),


        // with daterange + 0 params + orderby meta
        'filter\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/asc\/page\/([0-9])\/?$' => 'index.php?datelo=' . $wp_rewrite->preg_index(1) . '&datehi=' . $wp_rewrite->preg_index(2) . '&mixmeta=' . $wp_rewrite->preg_index(3) . '&order=asc&paged=' . $wp_rewrite->preg_index(4),
        'filter\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/asc\/?$'                => 'index.php?datelo=' . $wp_rewrite->preg_index(1) . '&datehi=' . $wp_rewrite->preg_index(2) . '&mixmeta=' . $wp_rewrite->preg_index(3) . '&order=asc',
        'filter\/([0-9]{8})\/([0-9]{8})\/by\/(_.+?)\/page\/([0-9])\/?$'      => 'index.php?datelo=' . $wp_rewrite->preg_index(1) . '&datehi=' . $wp_rewrite->preg_index(2) . '&mixmeta=' . $wp_rewrite->preg_index(3) . '&paged=' . $wp_rewrite->preg_index(4),
        'filter\/([0-9]{8})\/([0-9]{8})\/by\/(_.+)\/?$'                      => 'index.php?datelo=' . $wp_rewrite->preg_index(1) . '&datehi=' . $wp_rewrite->preg_index(2) . '&mixmeta=' . $wp_rewrite->preg_index(3),
        // with daterange + 0 params + orderby native
        'filter\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/asc\/page\/([0-9])\/?$'  => 'index.php?datelo=' . $wp_rewrite->preg_index(1) . '&datehi=' . $wp_rewrite->preg_index(2) . '&orderby=' . $wp_rewrite->preg_index(3) . '&order=asc&paged=' . $wp_rewrite->preg_index(4),
        'filter\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/asc\/?$'                 => 'index.php?datelo=' . $wp_rewrite->preg_index(1) . '&datehi=' . $wp_rewrite->preg_index(2) . '&orderby=' . $wp_rewrite->preg_index(3) . '&order=asc',
        'filter\/([0-9]{8})\/([0-9]{8})\/by\/(.+?)\/page\/([0-9])\/?$'       => 'index.php?datelo=' . $wp_rewrite->preg_index(1) . '&datehi=' . $wp_rewrite->preg_index(2) . '&orderby=' . $wp_rewrite->preg_index(3) . '&paged=' . $wp_rewrite->preg_index(4),
        'filter\/([0-9]{8})\/([0-9]{8})\/by\/(.+)\/?$'                       => 'index.php?datelo=' . $wp_rewrite->preg_index(1) . '&datehi=' . $wp_rewrite->preg_index(2) . '&orderby=' . $wp_rewrite->preg_index(3),
        // with daterange + 0 params w/o orderby
        'filter\/([0-9]{8})\/([0-9]{8})\/page\/([0-9])\/?$'                  => 'index.php?datelo=' . $wp_rewrite->preg_index(1) . '&datehi=' . $wp_rewrite->preg_index(2) . '&paged=' . $wp_rewrite->preg_index(3),
        'filter\/([0-9]{8})\/([0-9]{8})\/?$'                                 => 'index.php',

        // no daterange + 0 params + orderby meta
        'filter\/by\/(_.+?)\/asc\/page\/([0-9])\/?$' => 'index.php?mixmeta=' . $wp_rewrite->preg_index(1) . '&order=asc&paged=' . $wp_rewrite->preg_index(2),
        'filter\/by\/(_.+?)\/asc\/?$'                => 'index.php?mixmeta=' . $wp_rewrite->preg_index(1) . '&order=asc',
        'filter\/by\/(_.+?)\/page\/([0-9])\/?$'      => 'index.php?mixmeta=' . $wp_rewrite->preg_index(1) . '&paged=' . $wp_rewrite->preg_index(2),
        'filter\/by\/(_.+)\/?$'                      => 'index.php?mixmeta=' . $wp_rewrite->preg_index(1),
        // no daterange + 0 params + orderby native
        'filter\/by\/(.+?)\/asc\/page\/([0-9])\/?$'  => 'index.php?orderby=' . $wp_rewrite->preg_index(1) . '&order=asc&paged=' . $wp_rewrite->preg_index(2),
        'filter\/by\/(.+?)\/asc\/?$'                 => 'index.php?orderby=' . $wp_rewrite->preg_index(1) . '&order=asc',
        'filter\/by\/(.+?)\/page\/([0-9])\/?$'       => 'index.php?orderby=' . $wp_rewrite->preg_index(1) . '&paged=' . $wp_rewrite->preg_index(2),
        'filter\/by\/(.+)\/?$'                       => 'index.php?orderby=' . $wp_rewrite->preg_index(1),
        // no daterange + 0 params w/o orderby
        'filter\/page\/([0-9])\/?$'                  => 'index.php?paged=' . $wp_rewrite->preg_index(1),
        'filter\/?$'                                 => 'index.php'
    );
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}

1 Answer 1

2

I would argue this is micro-optimisation and your efforts will be better spent elsewhere - an extra 80 rules is a blink in the total WordPress runtime (they're just a foreach loop with a preg_match).

You are much better off making sure that any custom database tables are designed effectively, your queries are efficient, and that you leverage caching wherever you can.

Update: Start off with an in-depth look at WordPress' cache system. This series on caching at the page level also worth a read.

3
  • I realize this could be a separate question, but do you mind editing your answer to link to some sources that explain how I would effectively leverage caching for filter results pages? It may benefit other people stumbling across this question. Commented Jan 7, 2016 at 11:47
  • I don't know, generating the state machine to evaluate a regexp can be expensive. It is a pity php still don't offer a way to cache the state machines. If you have object caching on then it might actually take relatively long time. Agree it is probably early optimization for this question. Commented Jan 7, 2016 at 12:39
  • I would still think that parsing ~100 rewrite rules is still only ~1% of a total WordPress request. It's the actual filter system that needs to be TIGHT before OP should start worrying elsewhere. Commented Jan 7, 2016 at 12:49

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.