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>
Yes, RewriteEngine and RewriteBase have to be specified only once. Save this answer. Show activity on this post.
The <IfModule mod_rewrite. c>... </IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.
Open any web browser browser and type following the URL, 'localhost/check. php'. It will display the PHP version details and Apache Configuration. In Apache Configuration, search for the Loaded Modules section, and there you will find all the modules that are enabled.
Step 1 — Enabling mod_rewrite In order for Apache to understand rewrite rules, we first need to activate mod_rewrite . It's already installed, but it's disabled on a default Apache installation. Use the a2enmod command to enable the module: sudo a2enmod rewrite.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With