2

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.

1

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:

  • Thanks for the reply. – SimpliFixed Jul 21 '20 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.