The Apache web server is a very popular web server among various Linux distributions. It’s actively maintained by the Apache Software Foundation. This popularity is partly due to its modular design that allows you to customize the web server to fit your needs by enabling or disabling modules. One of those modules is the mod_rewrite module, which we will cover how to enable in this article.
Before we begin, you’ll need to ensure that you’re logged into your server via SSH.
Step 1 – Ensure Apache is installed and running
If you have not installed Apache at this point you may do so now by issuing the following command. If you have already installed Apache you may proceed to the next step.
yum install httpd -y
Once installed, start Apache using systemctl:
systemctl start httpd
You can check to make sure that Apache is running using the following command:
systemctl status httpd
You should see the following output if Apache is running.
systemd: Starting The Apache HTTP Server…systemd: Started The Apache HTTP Server.
Step 2 – Enable mod_rewrite
Most modern-day versions of CentOS such as CentOS 7 have mod_rewrite enabled by default, however, we can verify this using the following command which will list all currently loaded modules:
httpd -M | grep rewrite
If the mod_rewrite module is currently loaded you should see output similar to the following:
If the module is not in the output you’ll need to enable it. Generally, you can accomplish this in one of two ways in a CentOS environment. The first method is to add the following line to the 00-base.conf file using your favorite text editor. For this article, we will be using Nano, as you can see below.
Once the file is open, add or uncomment the following line:
The second method is to add or uncomment the same line used above to the httpd.conf file directly:
LoadModule rewrite_module modules/mod_rewrite.so
Once you have utilized either method to enable the module you’ll need to restart Apache:
systemctl restart httpd
After Apache has restarted the mod_rewrite module should be enabled and can be utilized via a .htaccess file.