10

The mod_rewrite rule is not working on the virtual host. I have wanted this for cakephp.

Is there setting to add in the virtual host file?

Nabil
  • 2,102
  • 1
  • 22
  • 29
user12gk21hkj
  • 217
  • 1
  • 2
  • 9

2 Answers2

25

If you are using latest apache version 2.4+ then here is the process how to enable mod_rewrite.

Go to etc/apache2/
Open apache2.conf using your favorite text editor.
Change

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

to

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Then save it. Remember you should have root permission to edit and save this file.
Now restart apache.

service apache2 reload

This process will enable mod_rewrite for apache server including virtual host.

You can also check if it's enabled or not. Create a php script

<?php    
phpinfo();    
?>

Load it from server. Under Loaded Modules section check if mod_rewrite is there or not. If it's there then it is enabled.

Nabil
  • 2,102
  • 1
  • 22
  • 29
  • Before I use this option all my rewrites were ending up at 404, however after updating `apache2.conf` and restarting `apache` 404 disappeared but all pages landed back on homepage! `phpinfo()` did not have `mod_rewrite` module enabled either. Then running CLI commnd (as suggested by Lonston below) everything gone golden! Why this solution did not enable `mod-rewrite` module for me but the CLI approad? Is this a combination of modifying `.conf` and running CLI command one after another to enable the module or either of these is actually enough? – Niladri Sarkar Jul 01 '16 at 09:47
6

Enable Rewrite using

# sudo a2enmod rewrite

Then restart the apcahe2 using

# service apache2 restart

Enable in Virtualhost by adding

AllowOverride FileInfo

Then restart the apcahe2 using

# service apache2 restart

example:

<Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            # I Have below line from none to FileInfo
            AllowOverride FileInfo
            Order allow,deny
            allow from all
    </Directory>
Babin Lonston
  • 6,186
  • 1
  • 23
  • 24