Enable mod_rewrite
You can enable any Apache module using the a2enmod command. So run the command below on your Ubuntu server:
# a2enmod rewrite
In case the module is already enabled on your server, you will get an alert message.
You must restart Apache once you make any change to its configuration. To do this, type the command below on a terminal window:
systemctl restart apache2
Your server is now ready to accept rewrite rules.
Setup your server to accept .htaccess files
You can set up URL rewrite rules directly on Apache’s configuration file. However, it is advisable to keep the rules in ‘.htaccess’ file on each website. Most content management systems rely on the ‘.htaccess’ file and it is created by default once you install the applications.
By default, Apache does not allow the use of ‘.htaccess’ file so you will need to edit the configuration of each website’s virtual host file by adding the following code:
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
For instance, you can edit the default virtual hosts that ships with Apache using a nano editor by typing the command below:
# vi /etc/apache2/sites-available/000-default.conf (for HTTP port 80)
# vi /etc/apache2/sites-available/default-ssl.conf (for HTTPS port 443)