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]+)


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


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

Thank you


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.


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.

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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