Bug 54543

Summary: Compile regex only when need
Product: Apache httpd-2 Reporter: David Saez <david>
Component: mod_rewriteAssignee: Apache HTTPD Bugs Mailing List <bugs>
Status: NEW ---    
Severity: enhancement Keywords: PatchAvailable
Priority: P2    
Version: 2.4.3   
Target Milestone: ---   
Hardware: All   
OS: All   
Attachments: Patch that add NPC new RewriteRule flag

Description David Saez 2013-02-10 17:20:33 UTC
Regular expressions are compiled (with ap_pregcomp) when parsing the configuration regardless if they will end being used or not.

If it makes sense i can submit a patch that will not compile the regex and substitute the calls to ap_regexec to calls to a function that will compile the regex if x->regex is null so only really need regex will be compiled

I think this could improve performance a bit, specially when using END/L flags and when the request matches the first rules, and will not affect performance in other cases. Anyway i could implement this as an option if you guys think that it will be better that way.
Comment 1 David Saez 2013-02-13 09:49:30 UTC
Created attachment 29945 [details]
Patch that add NPC new RewriteRule flag

When a RewriteRule has the NPC flag the rule will not be regex-compiled until the rule needs to be applied.

This flag only have sense and will only work for rules inside .htaccess files. Also the affected rule will be compiled each time is need so it should not be applied to rules that could be re-evaluated on the same request, usually this means that it should only be used when you are sure that the rule will not be evaluated again in the same request.

for example, if we are using some kind of php based CMS that resides on the root of the server and handles dynamic requests through index.php and uses SEO url's using mod_rewrite in a .htaccess file like this:

# avoid applying more rules to static content
RewriteRule ^(css|js|i)/.+   - [L,QSA]

# dinamic content
RewriteRule ^([a-z]+)/([0-9]+)$ /index.php?section=$1&id=$2 [END,NS,QSA]

# and a lot of similar rules ...

Now, all rules in the .htaccess file are always compiled, even for static content requests. We can avoid this and even avoid in many cases having to compile all rules for dinamic content using the NPC flag:

# avoid applying any rule to static content
RewriteRule ^(css|js|i)/.+   - [L,QSA]

# dinamic content
RewriteRule ^([a-z]+)/([0-9]+)$ /index.php?section=$1&id=$2 [END,NS,QSA,NPC]

in this case for static content matched by the first rule no other rule having NPC will be compiled thus saving the time need for regex compilation