Applying "RewriteEngine On" in .htaccess

Whenever an .htaccess function requires the rewrite engine to be on, the line that performs this is always included in every code example that I ever see; but no-one ever thinks to mention whether it is necessary to repeat it for every function (which is the impression that the examples give) or just write it once at or near the beginning of the file (which I think may be the more likely situation, possibly).

Could anyone clarify how, exactly, it needs to be applied? Thanks.

(Sorry if this is old ground, but I couldn’t find the answer by searching.)

You only need to put it in the .htaccess once, before any RewriteCond and RewriteRule lines.
Doesn’t necessarily have to be the first line, the following is perfectly acceptable

SetEnvIf REQUEST_URI htc$ no-gzip dont-vary
AddType text/x-component htc
Options +FollowSymLinks

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Here RewriteEngine On is on the 5th line of the .htaccess, and all RewriteCond and RewriteRule statements proceed it, so it’s fine :slight_smile:


What the RewriteEngine on/off statement does is to turn off/on the comment mode for mod_rewrite (RewriteEngine off starts comment mode; RewriteEngine on turns the comment mode off - using the “on” mode “merely” ensures that the comment mode hadn’t been turned on elsewhere).



Thank you to both of you. That’s helpful.