What does <IfModule> do in .htaccess?
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType application/javascript "access 1 week"
ExpiresByType text/css "access 1 week"
</IfModule>
I use a lot of these in my .htaccess files to set caching, deflate, Accept-encoding headers and stuff. I have noticed that if I delete the open and close tags, everything keeps working. What do these do, and is it safe to delete them, as long as everything seems to be working right?
<IfModule>
is simply a directive that tests the condition "is the named module loaded by apache httpd" (in your example mod_expires). It allows people to produce conditional based configuration for different installations where certain modules may be present or not.
Best practise is not to use it unless necessary. Ideally, you should know which modules your web server loads and thus which configuration directives are available.
You can always check the modules you web server is loading using apachectl -M
and apachectl configtest
before restarting your web server should reveal any directives that you have used without loading the relevant module.
It encloses directives that will only be applied if a particular module is available in the server. The module must either be statically compiled in the server, or it must be dynamically compiled and its LoadModule
line must be earlier in the configuration file. This directive should only be used if you need your configuration file to work whether or not certain modules are installed. It should not be used to enclose directives that you want to work all the time, because it can suppress useful error messages about missing modules.
In the following example, the MimeMagicFile
directive will be applied only if mod_mime_magic is available.
<IfModule mod_mime_magic.c>
MimeMagicFile "conf/magic"
</IfModule>