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

Apache mod_rewrite in .htaccess

· 52CE 我爱测

This should be a simple question. Checked Google but it listed nothing helpful. I use WP on one of my sites and it already generates a mod_rewrite section with: <IfModule mod_rewrite.c> RewriteEngine on wp_redirect_here </IfModul…

问题原文

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

This should be a simple question. Checked Google but it listed nothing helpful. I use WP on one of my sites and it already generates a mod_rewrite section with:

<IfModule mod_rewrite.c>
RewriteEngine on
wp_redirect_here
</IfModule>

Now I wrote some code to help lock down part of the backend and will contain:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond code_here
RewriteRule regEx_match_here
</IfModule>

I would like to leave the WP mod_rewrite block alone to avoid issues so if I have another section above can I just have:

RewriteCond code_here
RewriteRule regEx_match_here

Or do I need to have the open and close mod_rewrite and also turn on the rewrite engine like I have done before on other sites? I tried without the open and close and a engine on and it seems to be working just fine so I figure I should ask to make sure there are no issues.

已采纳解答

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

Yes, you can just have the RewriteCond / RewriteRule directives. You do not need to repeat the <IfModule mod_rewrite.c> wrapper or the RewriteEngine directive.

You only require the <IfModule mod_rewrite.c> wrapper here if your directives are "optional" to the running of your site and you intend to copy these same directives verbatim on many sites where mod_rewrite might not be available - which is the case with the WordPress code block. If mod_rewrite is not available then WordPress still "works", you just don't get the same "pretty" URLs.

See my answer to the following Webmasters question that goes into detail about why the <IfModule mod_rewrite.c> wrapper is "probably" not required.

With regards to the RewriteEngine directive... only the very last instance of the RewriteEngine directive does anything - and this controls the entire file. So if you repeat the RewriteEngine On directive, above the WordPress code block, it's not going to do anything anyway and is essentially ignored. In fact, it is arguably misleading since it gives the impression you can disable the rewrite engine just for your code block - you can't.

If you are manually writing all your .htaccess file then ideally you would just have one RewriteEngine On directive at the top (just because that's more readable - but the order does not strictly matter).

The reason why you typically see multiple RewriteEngine directives in a "WordPress" .htaccess file is that many of the directives are written by WordPress itself (or plugins) without knowledge of what other directives are already in the file - so adding RewriteEngine On is unavoidable. (Plus the fact that developers have a habit of blindly copy/pasting .htaccess code blocks and/or it's used out of habit perhaps from a misunderstanding to how Apache config files are processed.)

You could write a RewriteEngine Off directive at the very end of the file and this would disable the rewrite engine for the entire file, despite there being RewriteEngine On directives earlier in the file - these are essentially ignored. This is actually a quick way to "comment out" all the mod_rewrite directives, rather than manually using # in front of every directive.

So, if the # BEGIN WordPress block is still in place (with it's RewriteEngine On directive) - which you should not edit (as you've stated) - then there is no need to repeat the RewriteEngine On directive if you create your own custom redirects above this.

The same principles apply to the RewriteBase directive as well... the last instance wins and controls the entire file.


After writing this I've found an answer I wrote a while back to a related question, which covers some additional points:

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

资料来源与适用范围

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

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

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