How to Check If mod_rewrite is Enabled on CentOS


check if mod_rewrite enabled centos

mod_rewrite is a powerful Apache module that allows you to setup URL rewrites and redirection. It is useful when you move/delete web pages on your site, or restructure your entire site. However, it is essential to enable mod_rewrite module before you can add the rewrite rules to your Apache config. Let us look at how to check if mod_rewrite is enabled on CentOS.

 

How to Check If mod_rewrite is Enabled on CentOS

Here are the steps to check if mod_rewrite is enabled on CentOS

 

1. Check if module is installed

First, you need to check if mod_rewrite is already installed on your Apache server


$ ls /etc/httpd/modules | grep mod_rewrite

 

You will see something like the following output


mod_rewrite.so

 

It is unlikely that mod_rewrite is not installed. However, if you don’t get this output, then follow these steps to install mod_rewrite on CentOS

 

2. Check if mod_rewrite is loaded

Now that you have checked if mod_rewrite is installed or not, you need to check if it is enabled or not. You can do these with the following command


$ grep -i LoadModule /etc/httpd/conf/httpd.conf | grep rewrite

This will look for the line with “rewrite” in your Apache config file (httpd.conf). You should see the following output


LoadModule rewrite_module modules/mod_rewrite.so

If you see the above output, it means mod_rewrite is enabled on your Apache server.

Please note, there should be no ‘#’ sign at the beginning of your above output. ‘#’ sign is used to comment.

If you see the following output, it means mod_rewrite is not enabled


#LoadModule rewrite_module modules/mod_rewrite.so

In that case, open the config file using a text editor


$sudo vim /etc/httpd/conf/httpd.conf

Look for the line “#LoadModule rewrite_module modules/mod_rewrite.so” and remove ‘#’ sign at its beginning.

If you don’t find the line, just add it directly.

Save and close the file

 

3. Configure Apache config file

mod_rewrite allows you to setup redirection and URL rewrites using a .htaccess file, without modifying server config file. However, in order to do that, you need to enable .htaccess file to overwrite Apache config.

 

Open Apache config file


#sudo vim /etc/httpd/conf/httpd.conf

 

Look for <Directory /var/www/html> and change the directive AllowOverride from None to All.


. . .
<Directory /var/www/html>
. . .
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
. . .
</Directory>
. . .

Save and exit the file. Restart Apache to apply the changes


$ sudo systemctl restart httpd

 

4. Create .htaccess file

Next, create a .htaccess file in Apache’s root folder (/var/www/html)


$ sudo vi /var/www/html/.htaccess

 

Add the following line at the top, save and exit the file.


RewriteEngine On

This tells Apache to process the rules that follow.

 

5. Restart Apache Server

Restart Apache server to apply changes

$ sudo systemctl restart httpd

That’s it! mod_rewrite should be enabled in CentOS

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!