enable mod_rewrite for apache

How To Enable mod_rewrite For Apache in CentOS

mod_rewrite is a useful Apache module that helps you rewrite URL and redirect pages in Apache web server. Here’s how to enable mod_rewrite for Apache in CentOS, Fedora, Redhat Linux and other platforms.

 

How To Enable mod_rewrite For Apache in CentOS

Here are the steps to enable mod_rewrite for Apache in CentOS

 

1. Check if mod_rewrite is already installed

CentOS 7 and later versions have mod_rewrite installed and enabled by default. Open terminal and run the following command to check if mod_rewrite is already enabled in your system

$ httpd -M | grep rewrite

If you see the output as:

rewrite_module (shared)

it means mod_rewrite is already enabled in your CentOS linux.

Bonus Read : How to Redirect Domain Without Changing URL

 

2. Enable mod_rewrite in Apache CentOS

If mod_rewrite is not enabled then open Apache’s default configuration file

$ sudo vi /etc/httpd/conf/httpd.conf

Look for the line

#LoadModule rewrite_module modules/mod_rewrite.so

and remove the # sign at its beginning, to enable mod_rewrite

LoadModule rewrite_module modules/mod_rewrite.so

If # is not present at the beginning, it means mod_rewrite is already enabled

If you don’t find the above line, just add it to the file

LoadModule rewrite_module modules/mod_rewrite.so

Bonus Read : How to Redirect Page to Another Domain

 

3. Enable .htaccess file in Apache on CentOS

.htaccess file allows you to set up URL rewrite and redirection rules without changing Apache configuration file. To enable .htaccess file in Apache, look for <Directory /var/www/html> section and change the AllowOverride directive 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>
. . .

Bonus Read : How to Set Apache Headers Conditionally

 

4. Restart Apache Server

Restart Apache server with following command

$ sudo systemctl restart httpd

Now mod_rewrite is enabled for Apache in CentOS

Bonus Read : How to Configure SSL Certificate in Apache Web Server

 

5. Create .htaccess File in Apache (Optional)

If you want, you can also create a .htaccess file to

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

 

This will open a new file. Add the following line at the top

RewriteEngine On

Below this line, you can add URL rewrite rules. For example, if you want users to be able to access /about page on your website without typing .html extension (/about.html) then you can add the following rule

RewriteRule ^about$ about.html [NC]

 

Save and Exit the .htaccess file. Restart Apache Server. Now you can easily rewrite URL and redirect pages in Apache on CentOS.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!