Apache2 rewrite rules are not working

You have just edited your .htaccess on the root of your new site with RewriteRules and it’s not working. Something like this for a WordPress installation

RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

There are two reasons for this to happen. The mod_rewrite is not enabled by default and your configuration may not allow the use of .htaccess

Enabling mod_rewrite

As I usually don’t mess with Apache I often forget it has a command to enable modules. It’s just like this.

sudo a2enmod rewrite

Enabling .htaccess files

Edit your site configuration file under /etc/apache2/sites-available/. At the Directory entry for your site files you need to allow the reading of directives in .htaccess files.

If you were editing the default site

                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all

you would have to use AllowOverride FileInfo or even AllowOverride All. It’s your choice.

Get it to work

Finally you have to restart the Apache server to activate the changes

sudo service apache2 restart

That’s it!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.