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

Posted January 12, 2018 28.7k views
ApacheLAMP StackUbuntu 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

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

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

    Redirect permanent "/" ""


Contents of .htaccess
RewriteEngine on

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

1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

Found the solution here.

In /etc/apache2/apache2.conf changed:

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


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

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.

What is in 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?