user2994915
user2994915

Reputation: 13

.htaccess RewriteRule performance (large amount of RegEx)

I am using the following .htaccess rules to rewrite multiple urls on my website:

RewriteEngine On
RewriteRule ^privacy(\w|\s|\+|-|&|;|#)+policy/?$ index.php?page=privacy [NC]
RewriteRule ^([A-Za-z]+)(.*)+foo(\w|\s|\+|-|&|;|#)+bar/?$   index.php?page=$1 [NC]

The first rule works efficiently for the "privacy policy" page.

The second rule, however, results in a noticeably poor server response time. I believe the way this rule is written causes a delay in querying my database for the ([A-Za-z]+) expression. There is a large amount of RegEx that apply to this rule, and some of the urls contain "extra" words after the RegEx. For example:

www.example.com/North+America+Foo+Bar/

queries the database for "North" to rewrite the url to

www.example.com/index.php?page=North

This is the reason why I included (.*) in the rule, but I fear this is the culprit of the slow response time. Complicated, I know.

Any ideas or suggestions to improve this rule for faster performance? I would prefer keeping the www.example.com/Expression+Foo+Bar/ format because hundreds of urls are already indexed by search engines. =/

Thank you for your advice.

Upvotes: 1

Views: 293

Answers (1)

anubhava
anubhava

Reputation: 786091

You don't need to match the whole URI in 2nd rule. Try this rule with simplified regex:

 RewriteRule ^([a-z]+)(?=.*?foo.*?bar) index.php?page=$1 [NC]

Upvotes: 1

Related Questions