大网数据广告活动
网站运营与故障问答

Is Checking For mod_write Really Necessary?

· 52CE 我爱测

Recently I noticed that many people post .htaccess files here with: <IfModule mod_rewrite.c> Sometimes this even appears several times in the file! It obviously checks to see if mod_write is in fact enabled but the statements it …

问题原文

原帖链接 · Itai · CC BY-SA 3.0

Recently I noticed that many people post .htaccess files here with:

<IfModule mod_rewrite.c>

Sometimes this even appears several times in the file! It obviously checks to see if mod_write is in fact enabled but the statements it protects are necesary to those sites anyway. So if the IfModule fails, the website is broken anyway.

Is there an advantage in having the <IfModule mod_rewrite.c> fail rather than the RewriteRule statements below it?

mod_write now is so essential and ubiquitous that I do not check for it anymore. Might this cause a vulnerability of some kind?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

No, most of the time, checking for mod_rewrite is not necessary. In fact, it is often preferable to remove this check.

If the mod_rewrite directives are required by your site then you should not wrap them in a <IfModule mod_rewrite.c> container. Because if they are required and mod_rewrite is not available then the directives simply fail silently and your site continues to break in some other way (masking the underlying cause) and possibly exposing something you weren't expecting. Without the <IfModule> wrapper then the site would break instantly (and completely) with an easily identifiable error and nothing unexpected is exposed.

The only times when the <IfModule mod_rewrite.c> wrapper should be used is either:

  1. The site is designed to work with or without mod_rewrite. This is the case with WordPress. Without mod_rewrite the site still "works", you just don't get the "pretty" URLs.

Or

  1. You have directives from another module that are dependent on mod_rewrite having executed successfully. So, in this case you would wrap these other directives in a <IfModule mod_rewrite.c> wrapper. For example, setting an HTTP response header (with mod_headers) based on some property of the request that you have determined using mod_rewrite. In this case you might wrap the Headers directive in a <IfModule mod_rewrite.c> container.

Most of the time, if you know your server, then you don't need the <IfModule mod_rewrite.c> check - since you already know whether mod_rewrite is enabled or not. The only time when you do need it is if you are writing portable code to work on multiple servers and either condition #1 and/or #2 above are met.

Sometimes this even appears several times in the file!

And most of the time this is completely unnecessary. However, in defence of this behaviour, this often occurs when you have different plugins that edit .htaccess automatically and independently. The same is true for multiple RewriteEngine and RewriteBase directives.

For hand-written code you should never see this. For hand-written code this generally occurs through mindless copy/paste (which unfortunately seems to happen a lot with .htaccess directives).

Is there an advantage in having the <IfModule mod_rewrite.c> fail rather than the RewriteRule statements below it?

Only in the case of #1 or #2 above. Most of the time, no.

mod_write now is so essential and ubiquitous that I do not check for it anymore. Might this cause a vulnerability of some kind?

If it's essential for your site then there is no need to check for it. No vulnerability.

In fact, the opposite could even be true... if the mod_rewrite directives are essential then checking for the presence of mod_rewrite could even cause you more problems if mod_rewrite suddenly become unavailable for whatever reason. As mentioned above, your mod_rewrite directives would now silently fail (stress "silent" - no error), but the website may still continue to function without a server error being triggered but returning nonsense to the user (and search engine bots) with a 200 OK status. If the <IfModule> wrapper had been omitted then you would have been notified immediately of the problem. However, if this error was silenced it may be some time before the problem is discovered, by which time more serious damage may have already been done.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-02-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。