views:

26

answers:

1

Is it better to have a single RewriteRule with a bunch of RegEx or multiples Rules with fewer RegEx for the server to query? Will there be any performance differences?

Heres is an example a single rule with almost all RegEx groups as optional:

 RewriteRule ^gallery/?([\w]+)?/?([\w]+)?/?([\d]+)?/?([\w]+)/?$ /gallery.php?$1=$2&start=$3&by=$4 [NC]

Here are some of the rules lists that would replace the one above:

RewriteRule ^gallery/category/([\w]+)/$ /gallery.php?category=$1& [NC]
RewriteRule ^gallery/category/([\w]+)/([\d]+)/$ /gallery.php?category=$1&start=$2 [NC]
RewriteRule ^gallery/category/([\w]+)/([\d]+)/([\w]+)/$ /gallery.php?category=$1&start=$2&by=$3 [NC]
...
RewriteRule ^gallery/tag/([\w]+)/$ /gallery.php?category=$1& [NC]
RewriteRule ^gallery/tag/([\w]+)/([\d]+)/$ /gallery.php?category=$1&start=$2 [NC]
RewriteRule ^gallery/tag/([\w]+)/([\d]+)/([\w]+)/$ /gallery.php?category=$1&start=$2&by=$3 [NC]
...

I'll be glad to hear your options or personal experiences.

+2  A: 

Not that I've benchmarked anything, but I'd guess a single rewrite rule is more efficient than several if the functionality is equivalent.

Anyway, rewrite rules (even hundreds) rarely have a significant performance hit (I've read a benchmark on this a few years back), unless they proxy the request or stat paths.

In sum, I'd advise you to go with what's more readable to you.

In your specific example, I consider the single regex to be much more readable than the duplication. However, they are not equivalent -- the first always includes all the parameters. If that makes a difference (most likely it doesn't), it may be better to duplicate.

Artefacto
Thanks for your input, i think i'll use the single query as it will be easier to maintain, flexible and non-redundant.
Pablo