Monday, 8 January 2018

How to enable mod_rewrite on Apache on CentOS


The Apache mod_rewrite is a very powerful and sophisticated module which provides a way to perform URL manipulations. Using mod_rewrite one can perform all types of URL rewriting that one may need. This article will teach you how to enable apache module mod_rewrite on CentOS server.

Step 1: Check if the module exists

Before enabling the module mod_rewrite, we need to check if the module is already loaded in the httpd file.
Run the following command on your terminal.
# httpd -V
The output will be similar to the following,
Server version: Apache/2.2.15 (Unix)
Server built:   Jan 12 2017 17:09:39
Server's Module Magic Number: 20051115:25
Server loaded:  APR 1.3.9, APR-Util 1.3.9
Compiled using: APR 1.3.9, APR-Util 1.3.9
Architecture:   64-bit
Server MPM:     Prefork
threaded:     no
forked:     yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="logs/accept.lock"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
From the above result, we can find out the path of the apache config file (httpd.conf). From the result, we can say that the conf file path is /etc/httpd/conf/httpd.conf for our server.
To check if the module exists, run the command mentioned below,
# ls /etc/httpd/modules | grep mod_rewrite
If the module exists, you will get the following result.
The chances of not getting this result are rare, but if you don’t get this result you need to install mod_rewrite and compile apache with the module support.

Step 2: Check if the module is loaded

Now that we have verified that the module exists in the modules directory, let’s check if the module is loaded or enabled. To do this run the following command in the terminal,
# grep -i LoadModule /etc/httpd/conf/httpd.conf | grep rewrite
If you see the following result, it means that the module is enabled.
LoadModule rewrite_module modules/
If this result is commented like mentioned below, then remove the comment from the httpd.conf file, then save and exit the file.
#LoadModule rewrite_module modules/
But if you don’t get any of the above results, then add the following line to your httpd.conf file.
LoadModule rewrite_module modules/

Step 3: Configure httpd.conf file

Now that we have enabled the mod_rewrite module, next to use .htaccess to do URL rewriting using mod_rewrite, you need to allow each directory to override Apache’s global options.
Run the following command in your terminal,
# grep -i AllowOverride /etc/httpd/conf/httpd.conf
AllowOverride None
# AllowOverride controls what directives may be placed in .htaccess files.
    AllowOverride None
#    AllowOverride FileInfo AuthConfig Limit
# for additional configuration directives.  See also the AllowOverride
    AllowOverride None
    AllowOverride None
        AllowOverride None
Edit the httpd.conf file by locating <Directory /var/www/html> section and change the directive from None to All. Then save and exit the file.
That’s it, you have enabled the mod_rewrite module on your CentOS server.

No comments:

How to install asterisk secure fail2ban

1)  rpm -Uvh If this is already installed, skip this step. 2)  y...