Enable mod_rewrite for Apache 2

  1. Knowledge Base
  2. Apache
  3. Enable mod_rewrite for Apache 2

How to enable mod_rewrite for Apache 2

In order to enable mod_rewrite on your server, you can type the following command in the terminal:

sudo a2enmod rewrite

Restart apache2 after

sudo /etc/init.d/apache2 restart

or

sudo service apache2 restart

or as per new unified System Control Way

sudo systemctl restart apache2

Edit .htaccess File

Then, if you'd like to update .htaccess, you can use the following instructions:

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

If .htaccess file is placed in DocumentRoot, then it will redirect all traffic to an index.php file in the DocumentRoot unless the file exists.

So, let's say you have the following directory structure and http/www/ is the DocumentRoot

http/www/
    .htaccess
    index.php
    images/
        hello.png
    js/
        jquery.js
    css/
        style.css
includes/
    app/
        app.php

Any file that exists in httpdocs will be served to the requester using the .htaccess shown above, however, everything else will be redirected to http/www/index.php. Your application files in includes/app will not be accessible.

If none of the above works, then try editing /etc/apache2/sites-enabled/000-default and any other similar file in the directory:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

Change the AllowOverride None to AllowOverride All

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

 

That's it

Lets us know in the comments below if this solution worked for you.