How To Fix mod_rewrite If Not Working

First off, let me say that the following should work with Apache 2.2.12 and not necessarily any other version.

Are the Rules Wrong?

There are multiple reasons why your mod_rewrite rule(s) could be not working. The most obvious reason is that you somehow wrote the rules wrong. For a simple, clearly-written example, visit this site. If you can’t get the first example there to work, it’s fairly certain that the problem isn’t with the rewrite rule itself.

Is mod_rewrite Not Enabled?

If the problem isn’t with the rewrite rule itself, make sure mod_rewrite is enabled. If you’re using PHP, you can create the following PHP script and put it somewhere in your DocumentRoot:

<?php phpinfo();

If you visit that script in a browser, you should get a page that lists a bunch of configuration info. If you don’t see that, you probably have something going wrong with PHP.

While you’re on that page, search for “mod_rewrite”. If it’s there, you have mod_rewrite enabled. If not, you can run the following command:

sudo a2enmod rewrite

This will add the file rewrite.load to /etc/apache2/mods-enabled/. Now try your rule again, after restarting Apache:

sudo apache2ctl restart

Is mod_proxy Not Enabled?

Still not working? Try this:

sudo a2enmod proxy

This enables mod_proxy. To be honest, I’m not sure if this is actually necessary, but it doesn’t hurt anything so it’s probably worth a try. Remember to restart Apache afterward.

Is AllowOverride Set to None?

If mod_rewrite is still not working, the problem might lie in your /etc/apache2/sites-enabled/000-default file. You probably have a Directory directive for your DocumentRoot that looks something like this:

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

Change it to this:

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

Restart Apache. If none of those things work, I don’t know what to tell you.

Leave a Reply