The Apache module mod_rewrite is a flexible and powerful way to redirect traffic and rewrite URLs for a website. It is also a required module for many other software packages, including WordPress and the Laravel PHP framework. Learn how to install and enable mod_rewrite on a Cloud Server.
Requirements
- Cloud Server with Linux (CentOS 7 or Ubuntu 16.04).
- Apache installed and running.
Note: Apache is installed and running on a Standard Linux installation by default. If your server was created with a Minimal installation, you will need to install and configure Apache before you proceed.
CentOS 7
The mod_rewrite
Apache module is enabled by default on CentOS 7. If you find that it has not been enabled, you can enable it by opening the base module configuration file for editing:
sudo nano /etc/httpd/conf.modules.d/00-base.conf
Add the following line:
LoadModule rewrite_module modules/mod_rewrite.so
Save and exit the file.
Next, open the site's main Apache configuration file for editing:
sudo nano /etc/httpd/conf.d/example.com.conf
Find the main VirtualHost
command block:
<VirtualHost *:80>
ServerName example.com
<Directory "/var/www/example.com/html">
AllowOverride None
</Directory>
</VirtualHost>
In the Directory
section, change AllowOverride
to All
:
AllowOverride All
If there is no Directory
block in the VirtualHost
command block, add one which reads:
<Directory "/var/www/example.com/html">
AllowOverride All
</Directory>
Be sure to use the correct path to your website's document root.
Save and exit the file. Restart Apache for the changes to take effect:
sudo systemctl restart httpd
Ubuntu 16.04
To enable mod_rewrite
use the command:
sudo a2enmod rewrite
This will enable the module if it has not already been enabled. Restart Apache for the changes to take effect:
sudo systemctl restart apache2
Next, open the site's main Apache configuration file for editing:
sudo nano /etc/apache2/sites-available/example.com.conf
Find the main VirtualHost
command block:
<Directory /var/www/example.com/html/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change AllowOverride
to All
:
AllowOverride All
If there is no Directory
block in the VirtualHost
command block, add one which reads:
<Directory /var/www/example.com/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Be sure to use the correct path to your website's document root.
Save and exit the file. Restart Apache for the changes to take effect:
sudo systemctl restart apache2