mod_rewrite is an Apache module that provides URL rewriting functionality. It allows webmasters to modify URLs before the request is processed by the server. In CentOS, mod_rewrite may not be enabled by default. In this guide, we will go through the steps to enable mod_rewrite on CentOS.
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
Before enabling mod_rewrite, it is a good idea to check if it is already enabled. You can do this by using 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.
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: 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 5: 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”.
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.