<IfModule></IfModule> in .htaccess

by Yang Yang on December 2, 2011

I was debugging about some mod_rewrite errors caused by the .htaccess file on one of my sites and couldn’t solve the problem myself so I opted for a help thread on the SitePoint.com forum. Turned out it’s not the problem of my mod_rewrite rules in .htaccess but some incorrect file permissions that were causing the trouble.

Dklynn was very nice to help me in this regard and offered a gold advice that I thought I should share with you.

You Should NOT Use <IfModule></IfModule>. Why?

He pointed out that I should get rid of the <IfModule> conditional from the .htaccess file. It’s useless and a complete waste of server resources by all means. No excuses.

Considering the fact that all HTTP requests (including those to trivial assets such as .css, .js, .gif, .jpg, etc., thus each web page download would trigger about 10 or even more HTTP requests) to the Apache web server are handled by the rules in your .htaccess file, I think I should get rid of all such <IfModule> conditionals from all .htaccess files on all my sites, or the server would be doing useless check of “If Module xxx Is Enabled” every time a HTTP request is received…..

With a site receiving 1000 page visits per day, your server would have to do 10,000 such useless checks at the cost server resources and performance.

Many famous 3rd party scripts such as WordPress has <IfModule></IfModule> in the .htaccess file by default. This is to prevent potential errors should the module was not installed on the client’s production site / server. You should try and remove it. If the mod_rewrite and SEO friendly URLs are working, you can remove it without any problems.

This is pure gold advice offered by Dklynn. Check out his site about Apache mod_rewrite.

bla August 2, 2012 at 10:29 pm

Yeah. Fantastic. A micro optimization with no data to back up your statement.

“There’s someone who told me I should not do this” is always the best way to accomplish things. I don’t want to know how many people read this advise and do as you said. Maybe their hoster makes an update or change some config and then BAM Internal server error.

When they find out where the error is they only think god damn every tip from this guy is completly worthless.

+1 Really for many articles. But please stop with such statements without any data to backup your statement.

And really if all the ifmodule statements in your htaccess files bring your server down for gods sake consider a server upgrade.

Yang Yang August 20, 2012 at 12:41 pm

Thanks for the point. Not sure how it works for other people, but for me the directives in .htaccess are mostly pretty important ones that if they don’t work (such as if the modules aren’t installed or are misconfigured), the site will break any way. And 500 errors are perfect ways to tell of those problems.

For example, if mod_rewrite is suddenly turned off from the server, with <IfModule></IfModule> in .htaccess, the site will still work, but all the pages will have completely DIFFERENT URLs that is not rewritten or simply giving 404 errors.

This is absolutely disaster in terms of SEO. In this case, 500 errors become preferable.

In this regard, <IfModule></IfModule> is exactly like turning off the error report.

We turn off PHP error report on production site because it gives unnecessary information regarding the server that may be used by malicious users / attackers. However, .htaccess errors give no such information at all. I don’t see any reason why I should turn off this error report that does nothing more than notifying me of a critical module error which I should act on immediately.

Plus, it saves server resources.

Kavoir receives over 5,000 unique IP (blog, forum, and other static pages) per day that amounts to nearly 60,000 requests to the server every day. .Htaccess is not like PHP scripts whose output can be cached. Each and every in-coming request, even for static assets such as tiny .PNG images will go through .htaccess. Think about that and you will have an idea that, for larger sites, how much server resources complex .htaccess files are consumming. It’s not much but neither little.

If a server admin can inadvertently disable something that is essential for the site, this site is at bad hands any way……simply using <IfModule></IfModule> ain’t gonna save you from that.

Comments on this entry are closed.

Previous post:

Next post: