Search code examples
apache.htaccessmod-rewriteurl-rewritingwebserver

RewriteEngine On in <IfModule mod_rewrite.c> every time?


I'm wondering whether I can use RewriteEngine On only once within my htaccess when it is embed in IfModule mod_rewrite.c or do I have to use it every time because it is embeded?

See example below. Thanks

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} /?author   [NC]
RewriteRule .* - [F]
</ifModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} g=  [NC]
RewriteRule ^(.*)$ - [F,L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Solution

  • You should be able to put a single check around all your rewrite code.

    I suspect from your code that the site you are working on will NOT function properly or at all of mod_rewrite is not enabled. In such cases I would omit the checks for mod_rewrite completely and let the webserver fail if it is not enabled.

    If you should ever end up in a situation where your code is installed on a webserver without mod_rewrite enabled it will be a lot easier to debug and pinpoint the exact problem.