Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 762
WendiT Avatar asked Jul 14 '13 03:07

WendiT


People also ask

Do I need RewriteEngine on?

Yes, RewriteEngine and RewriteBase have to be specified only once. Save this answer. Show activity on this post.

What does IfModule mod_rewrite C mean?

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.

How do you check mod_rewrite is enabled or not?

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.

How do I enable mod rewrite?

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.


1 Answers

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.

like image 179
Michael Banzon Avatar answered Oct 15 '22 20:10

Michael Banzon