How to Enable mod_rewrite Apache module on CentOS

How to Enable mod_rewrite Apache module on CentOS

The mod_rewrite module is an integral part of the Apache HTTP server, offering a powerful and flexible way to manipulate URLs. It provides a robust and reliable solution for a wide range of URL-related tasks, such as URL redirection and URL shortening.

This tutorial will guide you through the process of enabling the mod_rewrite module on a CentOS system.

Step 1: Open the Terminal

The first step is to open the terminal on your CentOS machine. You can do this by clicking on the terminal icon or by using the keyboard shortcut “Ctrl + Alt + T”.

Step 2: Check if mod_rewrite is Already Enabled

CentOS 7 and later versions come with mod_rewrite installed and enabled by default. To check if mod_rewrite is enabled on your system, open the terminal and run the following command:

sudo httpd -M | grep rewrite

If mod_rewrite is already enabled, you will see an output similar to the following:

rewrite_module (shared)

If you do not see any output or if mod_rewrite is not listed, it means that it is not enabled.

See also  How to Install LAMP on CentOS 7 / RHEL 7

Step 3: Enable mod_rewrite

To enable mod_rewrite, you need to edit the Apache configuration file. You can do this by using the following command to open the file in a text editor:

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

In the file, look for the following line:

#LoadModule rewrite_module modules/mod_rewrite.so

Remove the “#” at the beginning of the line to uncomment it:

LoadModule rewrite_module modules/mod_rewrite.so

Save the file and exit the text editor.

Step 4: Create .htaccess File in Apache

If you want, you can also create a .htaccess file to set up URL rewrite rules. Open a new .htaccess file with the following command:

sudo vi /var/www/html/.htaccess

At the top of the file, add the following line to enable the rewrite engine:

RewriteEngine On

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

RewriteRule ^about$ about.html [NC]

Save and exit the .htaccess file, then restart the Apache server.

See also  How to Configure PostgreSQL to Listen from Any IP Address on CentOS 6.2

Step 5: Restart Apache

After enabling mod_rewrite, you need to restart Apache for the changes to take effect. You can do this by using the following command:

sudo service httpd restart

Step 6: Verify the Installation

To verify that mod_rewrite is enabled, you can create a test file with some rewrite rules and test it in your web browser. For example, you can create a file named “test.php” with the following content:

<?php
echo "Hello, World!";
?>

Then, create a file named “.htaccess” in the same directory with the following content:

RewriteEngine On
RewriteRule ^hello$ test.php

This will rewrite the URL “http://yourdomain.com/hello” to “http://yourdomain.com/test.php”.

See also  How to Install Bind Chroot DNS Server on CentOS 6.2

Save the file and access “http://yourdomain.com/hello” in your web browser. If mod_rewrite is enabled, you should see the message “Hello, World!”.

Commands Mentioned:

  • sudo – a command that allows users to run programs with the security privileges of another user, typically the superuser.
  • httpd – the Apache web server daemon.
  • vi – a text editor for Unix-like systems.
  • service – a command to control system services.

Conclusion:

In this guide, we have gone through the steps to enable mod_rewrite on CentOS. By enabling mod_rewrite, you can use URL rewriting to modify URLs before the request is processed by the server. If you have any questions or suggestions, feel free to comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *