What is .htaccess RewriteRule best practice?
Is it better to have one RewriteRule with a group of RegEx or multiples of rules with fewer RegEx to query the server? Will there be any performance differences?
Here's an example of a single rule where almost all RegEx groups are optional:
RewriteRule ^gallery/?([\w]+)?/?([\w]+)?/?([\d]+)?/?([\w]+)/?$ /gallery.php?$1=$2&start=$3&by=$4 [NC]
Here are some of the lists of rules that would replace the 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 will be glad to hear your options or personal impressions.
a source to share
Not that I am comparing anything, but I would suggest that one rewrite rule is more efficient than several if the functionality is equivalent.
Anyway, rewrite rules (even hundreds) rarely have significant performance gains (I read this metric years ago) unless they proxy request paths or stat.
All in all, I would advise you to go with whichever reads best for you.
For your specific example, I believe a single regex will be more readable than duplication. However, they are not equivalent - the first one always includes all parameters. If it matters (most likely it doesn't), it might be better to duplicate.
a source to share