Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have the following:

RewriteRule ([a-zA-Z0-9]{5})?/load_page/([0-9]+)

Which arrangement would be optimal for the regex:

RewriteRule ^/([a-zA-Z0-9]{5})?/load_page/([0-9]+)

OR

RewriteRule ^/load_page/([a-zA-Z0-9]{5})?/([0-9]+)

OR

RewriteRule ^/([a-zA-Z0-9]{5})?/([0-9]+)/load_page

Thank you

share|improve this question

2 Answers 2

up vote 2 down vote accepted

I don't think in your case there's a notable difference.

Want to be sure? Benchmark all of them with Apache Benchmark, Siege, JMeter or whatever benchmarking tool you want to use or are familiar with.

share|improve this answer

Janne is likely right that the difference is negligible and that you should not optimize without benchmarking.

To answer the question anyway, it should depend on what URLs are requested. I would guess that most URLs requested don't have a path that starts with /load_page. In that case, it's likely that the regexp starting with ^/load_page will be slightly faster because it can rule out many non-matching strings quickly by comparing the first few characters and not having to backtrack. So if you request /foo, it will compare the f to l, see a mismatch and bail after comparing one or two characters.

With the other regexps, it would see /foo and think "I'm seeing some fine alphanumerics" and only abort after realizing that the rest of the regexp doesn't match.

share|improve this answer
    
That's what I was thinking, but wasn't sure if the URLs are matched left-to-right or right-to-left or if another algorithm is used. Probably best way to go would be to benchmark them –  Catalin Jul 19 '11 at 12:21
    
What little experience I have of implementing regexp engines, it was left-to-right. –  Henrik N Jul 19 '11 at 14:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.