What is .htaccess RewriteRule best practice?
- by Pablo
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.