The Apache mod_rewrite module is a rewriting engine based on defined rules. The Apache rewrite engine maps a URL to a directory path as well as to other URLs. In this tutorial, you will learn, how to enable the Apache mod_rewrite module and configure VirtualHost to use .htaccess files available under the document root.
Sometimes you faced issues the Apache server is not reading your .htaccess or Apache is not rewriting URLs while we are using correct rewrite rules in configuration files. This happens due to the Apache rewrite module is not being enabled. When we installed a fresh Apache server mod_rewrite is not enabled by default on your server, So to use rewrite configurations you need to manually enable the mode_rewrite module on your system.
Enable Apache2 mod_rewrite Module
We use
sudo a2enmod rewrite
Enable .htaccess for VirtualHost
After enabling the Apache rewrite module, now you need to add “AllowOverride All” in your VirtualHost configuration file.
1 2 3 4 5 6 7 8 | <VirtualHost *:80> ServerName www.example.com DocumentRoot /var/www/html <Directory /var/www/html> AllowOverride All </Directory> </VirtualHost> |
This setting can also be enabled globally by editing the Apache main configuration file.
1 2 3 4 | <Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride All </Directory> |
Restart Apache2
After enabling the mod_rewrite module in Apache you also need to reload the Apache2 server to reload all configurations to the running environment.
sudo systemctl restart apache2
2 Comments
“…in your VirtualHost configuration file.”
“…by editing the Apache main configuration file.”
Would you mind sharing the location of these?
/etc/apache2/apache2.conf is the main configuration file
the VirtualHost files are in /etc/apache2/sites-enabled