Case-insensitive add_rewrite_rules in WordPress functions

Been hitting my head against the wall on this for a while. If I have a add_rewrite_rules like so:

    $aNewRules = array('my-books/?$'  => 'index.php?pagename=my-books');

This works correctly; going to http://example.com/my-books/ shows http://example.com/index.php?pagename=my-books.

However, this is case sensitive – going to “/My-bOoKs/” does not trip the rule (and thus shows the 404 page instead).

Is there an easy way to just mark it as case insensitive? I only make links with the lower case, sure, but users may add a capital on their own and I’d hate to lose the traffic to a 404 page.

Thanks!
Alex

2 Answers
2

As the answer below mentions, it is not possible to pass a flag to add_rewrite_rule(); however, it is possible to use an inline modifier. In your example, you would do this:

$aNewRules = array('(?i)my-books/?$' => 'index.php?pagename=my-books');

(note the (?i) in the regular expression).

The advantage of this approach is that your rewrite rules are much cleaner and more performant.

See this page on regular expression modifiers for more information.

Leave a Comment