Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many RewriteRules can you have in .htaccess without trouble?

Simple question. Is there a limit as to how many RewriteRules I can have in my .htaccess or can I put a zillion of them in there without Apache going all funky on me?

Is there a relative limit where the server simply dives because there are to many rules to iterate through?

Thanks!

like image 671
Willem Avatar asked Mar 25 '09 14:03

Willem


People also ask

What is $1 in htaccess?

In your substitution string, $1 contains the contents of the first set of parens ( hello ), while $2 contains the contents of the second set ( there ). There will always be exactly as many "dollar" values available in your substitution string as there are sets of capturing parentheses in your regex.

How long does it take for htaccess to change effect?

htaccess files follow the same syntax as the main configuration files. Since . htaccess files are read on every request, changes made in these files take immediate effect.

What is htaccess rule?

htaccess is a configuration file used by the Apache web server. . htaccess rules override global settings for the directory in which the file is placed. You may find that . htaccess files are created automatically on your server, when you install popular web applications like WordPress, Drupal, and Magento.

How do I know if htaccess rewrite is working?

First, visit https://htaccess.madewithlove.be. Then, enter your site's url and your . htaccess rewrite rule in the appropriate fields. Click Check Now.


2 Answers

You have to know that the .htaccess configuration files are being processed on every request.

So if you have a .htaccess file with 1000 rules, the worst case is that every 1000 rules are tested every time a request hits this directory.

Therefore you should use a structure where a rule matches a request as early as possible. Rules that handle more frequent requests should appear before those that are less frequent and determine the processing (see L flag). Read about the ruleset processing to know how the rules are being processed (see also RewriteLogLevel direcitve).

Another factor are the regular expressions: Better use “simple” and efficient regular expressions than ambiguous or complex ones. You should look into the how regular expressions are interpreted and processed to avoid costly ones and get the most out of them.

like image 108
Gumbo Avatar answered Nov 15 '22 11:11

Gumbo


If your RewriteRules include several is existing file (-F) or is existing url (-U) flags, since those are subrequests, you could see a performance hit. Outside of those, I haven't experienced a situation where several RewriteRules start adversely effecting performance and my current implementation has a good amount.

You can limit the need to iterate through all the RewriteRules by just ordering them in such a way that the more-expensive checks are done later on in the process (if possible), that way earlier conditionals can short circuit out of your rewrite logic and save you the computation of the more expensive rules later on in the process.

like image 25
Kyle Walsh Avatar answered Nov 15 '22 11:11

Kyle Walsh