Forum Moderators: phranque

Message Too Old, No Replies

.htaccess or httpd.conf for mod_rewrite

         

georgiek50

11:04 pm on Mar 20, 2004 (gmt 0)

10+ Year Member



Thanks to all the great posts in these forums I got the job done in a matter of minutes but I put the rules in my .htaccess file. Since I am on a dedicated server and have access to my httpd.conf would there be a large performance increase to put the rules in that file instead? If so, can someone tell me where exactly I should put the code, and should I enclose it in the <IfModule...> tags. Thanks

jdMorgan

2:13 am on Mar 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



georgiek50,

In general, mod_rewrite code placed in httpd.conf executes much faster than similar code placed in .htaccess. The reason for this is that mod_rewrite code in httpd.conf is compiled as the server is restarted, whereas code in .htaccess must be interpreted for each and every HTTP request that accesses resources in the directory in which that code resides. So, it is usually much more efficient to place the code in httpd.conf. On the other hand, each time you make a change to your code in httpd.conf, you must restart your server to recompile the code. So, some people develop in .htacccess and deploy to httpd.conf only after extensive testing.

Be aware also that there are subtle differences in the two contexts. In particular, if your .htaccess code is written without a RewriteBase directive, you will have rules that look like this example:


RewriteRule ^somefile\.ext$ /someotherfile.ext [L]

In httpd.conf, that code would have to be modified to look like this:

RewriteRule [b]^/s[/b]omefile\.ext$ /someotherfile.ext [L]

The difference is that in httpd.conf, RewriteRule "sees" the leading slash on the requested URL, whereas in .htaccess, the leading slash is stripped off before the rule can test it.

If you intend to use your code on multiple servers, where some will have mod_rewrite installed and others won't, then you will want to place your code within an IfModule container. Otherwise, where you place it depends on whether it applies only to a single server, or to a single virtual server, versus applying to multiple virtual servers. So, you'll need to ask yourself whether the rules apply to one or more servers on your machine, and place the code in the appropriate container.

Jim

georgiek50

6:05 pm on Mar 21, 2004 (gmt 0)

10+ Year Member



Since I am using the whole server for one site, can I just append the <ifmodule...> to the end of the httpd.conf with all the rules? Thanks for the explanation btw.