mod_rewrite not working on Apache on Ubuntu 16.04 with self-signed certificate

January 12, 2018 4.3k views
Apache LAMP Stack Ubuntu 16.04

I have a LAMP stack Droplet on Ubuntu 16.04. I've followed the tutorial to setup mod_rewrite here. If I type sudo a2enmod rewrite it indicates Module rewrite already enabled. However, the rewrite rules do not work.

This answer indicates that this might has to do with my VirtualHost *:443 settings, is that so?

Contents of /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

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

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
    </IfModule>

    Redirect permanent "/" "https://188.166.60.249/"

</VirtualHost>

Contents of .htaccess
RewriteEngine on

RewriteCond %{REQUESTFILENAME} !-d
RewriteCond %{REQUEST
FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

1 comment
2 Answers

What is in error log ?

/var/log/apache2/error.log
  • There are no errors there with the timestamp of when this happens. If I add index.php to the URL, my application is working (throwing errors which are on the app side) so it's clear mod_rewrite does not have effect. Any ideas?

Found the solution here.

In /etc/apache2/apache2.conf changed:

<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>

Last question is if this does not impose security risks..?

  • No it allows Apache2 configuration via htaccess in /var/www/html/ instead of in /var/www.
    If your app is in /var/www/html/ you could change path in /etc/apache2/apache2.conf from /var/www to /var/www/html. Or delete Directory section in apache2.conf and leave only in vhost conf.

    Hint: if you can, disable AllowOverride and add rewrite rules to vhost config in directory section you can gain some performace boost because if AllowOverride is on, then Apache2 search in every directory within DocumentRoot for htaccess in every request.

Have another answer? Share your knowledge.