.htaccess doesn't work on ubuntu droplet

May 5, 2015 11.8k views
Apache Configuration Management Ubuntu

I installed LAMP on my droplet and then I followed this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite and I used the contents for the file from my old web hosting, so I know it works:
RewriteEngine On
RewriteCond %{REQUESTFILENAME} !-f
RewriteCond %{REQUEST
FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

However, if I enter for example http://cyan.red/index it comes up with:
Not Found

The requested URL /index was not found on this server.

4 Answers

By default the LAMP install will disable usage of the .htaccess file.

You need to edit the configuration file for your virtual host.

 $ nano /etc/apache2/sites-available/000-default.conf

Change the <VirtualHost> directive to look something like this. The important part is AllowOverride All, as this tells Apache that the directory can override default settings using a .htaccess file.

<VirtualHost *>
     ServerName cyan.red
     ServerAlias www.cyan.red
     DocumentRoot /var/www/html

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

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

Now make sure the website is enabled.

  $ a2ensite 000-default.conf

Also make sure that the rewrite mod is enabled.

  $ a2enmod rewrite

Now restart Apache

   $ service apache2 restart
  • Thanks, that worked! :D
    I had put the bit in the wrong place in the config file!

You know this command $ a2enmod rewrite is not mentioned in any DO tutorial, but without it I couldn't enable .htaccess, now can, thanks

If multiple domains are hosted, you need to add in each virtualhost, where .htaccess is to be enabled.

For example, to enable .htaccess for example.com and test.com, edit VirtualHosts:

/etc/apache2/sites-available/example.com.conf
and
/etc/apache2/sites-available/test.com.conf


<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    <Directory /var/www/example.com/html>
        Options FollowSymLinks
        AllowOverride all
        Require all granted
     </Directory>


    DocumentRoot /var/www/example.com/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

and similarly, for test.com


<VirtualHost *:80>
    ServerAdmin admin@test.com
    ServerName test.com
    ServerAlias www.test.com
    <Directory /var/www/test.com/html>
        Options FollowSymLinks
        AllowOverride all
        Require all granted
     </Directory>


    DocumentRoot /var/www/test.com/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Also if ssl is enabled, you need to edit its configuration as well:


<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/html
    <Directory /var/www/example.com/html>
        Options FollowSymLinks
        AllowOverride all
        Require all granted
     </Directory>





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

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

Have another answer? Share your knowledge.