23

I'm trying to get a PHP routing library set up. They give this example for a .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

I couldn't get this to work, so I tried enabling mod_rewrite, but it says "Module rewrite already enabled".

Why is it not working properly? Thanks! I'm running Ubuntu Precise 12.04, and apache2.2.22. (Checked for any updates)

EDIT: A couple more details, it's a PuPHPet vagrant build, rewrite should be enabled.

5 Answers 5

41

You need to allow the overwrite.

<Directory "/path/to/document/root/">
  AllowOverride All

</Directory>
1
  • 7
    This is unnecessarily permissive. Only AllowOverride is necessary for the configuration in question. Allow from All has nothing to do with the question and may not be appropriate for @randomdev's environment.
    – Mark
    Dec 19, 2015 at 16:15
21

First of all, set your httpd configuration to this (the path may differ with one another. In my ubuntu it's placed at /etc/apache2/sites-available/default):

DocumentRoot /var/www

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

After that, you should enable mod_rewrite with this command:

sudo a2enmod rewrite

The last one, restart your apache service:

sudo service apache2 restart

To ensure that, you can check it again from phpinfo in Configuration > apache2handler > Loaded Modules there must be written mod_rewrite and it means mod_rewrite is enabled.

2
5

I had the similar problem, but the other answers did not helped me. This line at the begining of .htaccess solved my problem:

Options +FollowSymLinks -MultiViews
1
  • This did not help me either. Tried this and everything else and still got 404, a2enmod says module already enabled and I also have restarted my whole server a few times.
    – bxyify
    Nov 20, 2020 at 9:09
1

My problem was that I didn't have RewriteEngine on set early on in the file.

<VirtualHost *:80>
        ServerName &URL&
        ServerAlias *.&URL&

        ServerAdmin &EMAIL_ADDRESS&

        DocumentRoot            /var/www/&URL&/public

        RewriteEngine on

        # LogLevel: Control the severity of messages logged to the error_log.
        # Available values: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the log level for particular modules, e.g.
        # "LogLevel info ssl:warn"
        LogLevel                debug

        ErrorLog                /var/www/&URL&/storage/logs/apache2_error.log
        TransferLog             /var/www/&URL&/storage/logs/apache2_transfer.log

        <FilesMatch "\.(cgi|html|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>

        # <------------  Here I used to have `RewriteEngine on`

        # Handle Front Controller...
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
        RewriteRule ^ /index.php [L,QSA]

</VirtualHost>

I dont know why but it helped when I placed RewriteEngine on higher up.

0

In my case, I moved one site from a different partition to the Apache's partition, but I had to create a symlink for the old location pointing this folder. The problem was that DocumentRoot was not updated, kept pointing to the old path, that is a link now.

PS: It is NOT a comercial website, only an intranet for "domestic" use.

So MOD_REWRITE stopped working because DocumentRoot was not poiting to the new folder path, but to the link end.

[ partition A (Apache) ] <-- Folder moved  <-- [ FOLDER partition B ]

[ partition A (Apache) ] --> Symbolic link --> [ (link) partition B ]

When I updated DocumentRoot with the real folder that now is in "A", it solved the problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .