Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite - Where to put it

.htaccess, <directory>, <virtualhost>

         

Nutter

1:47 pm on Mar 4, 2005 (gmt 0)

10+ Year Member



Since upgrading from a shared plan to a VPS, I now have the option of where to put mod_rewrite directives. Reading the documentation on mod_rewrite, it appears that it's faster to put the commands in httpd.conf; which makes sense. I've put them in a <directory> block for the root of this particular site. It works, but would it be better to go in the <VirtualHost> group?

I guess my question is: has anyone done any benchmarking? Or, anyone have any hints on how to benchmark?

Thanks,
- Ryan

jdMorgan

3:57 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The issue with mod_rewrite performance in httpd.conf versus .htaccess is that mod_rewrite code in http.conf is compiled at server start-up and directly-executed per HTTP request, while the same code in .htaccess is interpreted per HTTP request. Therefore, code in httpd.conf runs significantly faster than the same code in .htaccess. The only negative in httpd.conf is that the server must be restarted each time you want to change your code in httpd.conf. For this reason, many people develop and tweak in .htaccess, and then adapt the code for use in httpd.conf (there are a few subtle differences, notably that the leading slash of local URL-paths is stripped from the path seen by RewriteRule in .htaccess.)

As to which container to use, that depends on the nature and purpose of the rules. If you have rules that apply to an entire virtual host, put them in the <VirtualHost> container(s). If the rules apply only to a specific directory or directory-path on a specific vhost, then put them in a <directory> container. Feel free to do both as needed.

Jim

Nutter

5:30 pm on Mar 4, 2005 (gmt 0)

10+ Year Member



So...

If I want it to apply domain wide, put it in VirtualHost; if I want it to apply to only a specific directory (domain.com/subdir); put it in a <Directory>. Any difference other than that?

Will one overrule the other? Which will get used for domain.com/subdir, <VirtualHost domain.com> or <Directory /home/domain/subdir>?

And, last question, there is a command telling Apache to ignore .htaccess, but I can't remember what it is. It seems that not having to step through subdirectories looking for .htaccess would speed up the process as well. Any thoughts?

Nutter

5:44 pm on Mar 4, 2005 (gmt 0)

10+ Year Member



Ok, follow up -

When I put the code in <VirtualHost> I get a 400 error, but it appears to work when in a <Directory>. The code should be exactly the same, correct. Although, it appears I don't need RewriteBase, since it causes an error upon apache restart if it's in the VirtualHost.