What Is mod_rewrite
What Is Mod_rewrite The mod_rewrite module uses a rule-based overwriting mechanism and a regular PCRE parser to rearrange the requested URLs upon request. By default, mod_rewrite maps the URL to the file system path. However, it can also be used to redirect one URL to another URL or to invoke an internal proxy alert.
mod_rewrite provides a flexible and powerful way to manipulate URLs with an unlimited number of rules. Each rule can have an unlimited number of conditions, bound rules, allowing you to rewrite URLs based on server variables, environment variables, HTTP headers, or timestamps.
mod_rewrite runs on the full URL path, including the path-info section. The overwrite rule can be invoked in httpd.conf or in .htaccess files. The path generated by the overwrite rule may involve a series of queries or may lead to internal sub-processing, external request redirection, or internal proxy throughput.
Set mod_rewrite
Enable mod_rewrite:
sudo a2enmod rewrite
sudo systemctl restart apache2
Configure rule recognition from the .htaccess
file
sudo nano /etc/apache2/sites-available/000-default.conf
Inside this file, you will find a block starting with <VirtualHost *: 80>
. Within this block, add the following new block so that the configuration file looks like this
<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
. . .
</VirtualHost>
To enable the new rules we need to restart the Apache server
sudo systemctl restart apache2
You can now add rewrite rules to the .htaccess
file most commonly located in the root directory of the web application.