How To Configure mod_rewrite for Apache on CentOS 7

Centos 7Apache Web ServerFile System

In this article, we will learn about how to configure the mod_rewrite for Apache on CentOS 7, Apache is a very popular web server which allows us to customize and enable its capabilities and disable them. if we now needed more, the Apache administrator has permissions to change according to our needs of the web application.


  • We needed a Linux machine installed with CentOS 7.
  • We need a non-root user with sudo permissions.

Installing the Apache Web Server

We will install the Apache Web Server using yum as, Apache is available with the default packages with the management utility of the CentOS repository.

The Apache daemon is called as httpd in CentOS, we needed to install the Apache using the yum command as this is a default package from the CentOS package management utility.

Below is the command to install the Apache Web Server

$ sudo yum install httpd
sudo yum install httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base:
* extras:
* updates:
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
Package                Arch             Version                   Repository          Size
httpd                x86_64           2.4.6-40.el7.centos.4       updates             2.7 M
Installing for dependencies:
apr                   x86_64          1.4.8-3.el7                 base                103 k
apr-util              x86_64          1.5.2-6.el7                 base                92 k
httpd-tools           x86_64          2.4.6-40.el7.centos.4       updates             83 k
mailcap               noarch          2.1.41-2.el7                base                31 k
Transaction Summary
Install 1 Package (+4 Dependent packages)
Total download size: 3.0 M
Installed size: 10 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): apr-util-1.5.2-6.el7.x86_64.rpm                                  | 92 kB 00:00:00
(2/5): apr-1.4.8-3.el7.x86_64.rpm                                       | 103 kB 00:00:00
(3/5): mailcap-2.1.41-2.el7.noarch.rpm                                  | 31 kB 00:00:00
(4/5): httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm                     | 83 kB 00:00:00
(5/5): httpd-2.4.6-40.el7.centos.4.x86_64.rpm                           | 2.7 MB 00:00:01
Total                                                          2.4 MB/s | 3.0 MB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : apr-1.4.8-3.el7.x86_64 1/5
Installing : apr-util-1.5.2-6.el7.x86_64 2/5
Installing : httpd-tools-2.4.6-40.el7.centos.4.x86_64 3/5
Installing : mailcap-2.1.41-2.el7.noarch 4/5
Installing : httpd-2.4.6-40.el7.centos.4.x86_64 5/5
Verifying : httpd-tools-2.4.6-40.el7.centos.4.x86_64 1/5
Verifying : apr-1.4.8-3.el7.x86_64 2/5
Verifying : mailcap-2.1.41-2.el7.noarch 3/5
Verifying : httpd-2.4.6-40.el7.centos.4.x86_64 4/5
Verifying : apr-util-1.5.2-6.el7.x86_64 5/5
httpd.x86_64 0:2.4.6-40.el7.centos.4
Dependency Installed:
apr.x86_64 0:1.4.8-3.el7                               apr-util.x86_64 0:1.5.2-6.el7
httpd-tools.x86_64 0:2.4.6-40.el7.centos.4             mailcap.noarch 0:2.1.41-2.el7

Once the package is installed and we needed to start the Apache daemon using the systemctl command utility.

$ sudo systemctl start httpd

Once the httpd daemon is started, we will check the status of the httpd services with the below command –

$ sudo systemctl status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2016-11-09 12:52:17 IST; 4s ago
Docs: man:httpd(8)
Main PID: 29600 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─29600 /usr/sbin/httpd -DFOREGROUND
├─29601 /usr/sbin/httpd -DFOREGROUND
├─29602 /usr/sbin/httpd -DFOREGROUND
├─29603 /usr/sbin/httpd -DFOREGROUND
├─29604 /usr/sbin/httpd -DFOREGROUND
└─29605 /usr/sbin/httpd -DFOREGROUND
Nov 09 12:52:17 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
Nov 09 12:52:17 localhost.localdomain httpd[29600]: AH00558: httpd: Could not reliably
Nov 09 12:52:17 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

Verify the mod_rewrite directive

Since the default the mod_rewrite is enabled, so to verify we will run the below command using the command utility httpd with -t -D DUMP_RUN_CFG option which displays all the modules loaded with current configuration.

$ httpd -t -D DUMP_RUN_CFG
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex authdigest-client: using_defaults
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
PidFile: "/run/httpd/"
User: name="apache" id=48
Group: name="apache" id=48

We can edit the configuration file to enable or disable the rewrite_modules.

$ sudo vi /etc/httpd/conf.modules.d/00-base.conf
# This file loads most of the modules included with the Apache HTTP
# Server itself.
LoadModule access_compat_module modules/
LoadModule actions_module modules/
LoadModule alias_module modules/
LoadModule allowmethods_module modules/
LoadModule auth_basic_module modules/
LoadModule auth_digest_module modules/
LoadModule authn_anon_module modules/
LoadModule rewrite_module modules/

Once we enable the rewrite_module we needed to save the file and quit

We have the apply the configuration changes made by restarting the Apache server

$ sudo systemctl restart httpd

Configuring the .htaccess File

The .htaccess file will define the directives for the Apache Web server, which includes the RewriteRules on the domain without changing the configuration files in the Linux and the files preceding with a dot(.) is treated as hidden files.

We needed to edit and add the AllowOverride setting in the Apache directives.

$ sudo nano .etc.httpd/conf.httpd.conf
# Further relax access to the default document root:
<Directory "/var/www/html">
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
# The Options directive is both complicated and important. Please see
# for more information.
Options Indexes FollowSymLinks
# 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
# Controls who can get stuff from this server.
Require all granted

Save the configuration file and apply the changes with the below command –

$ sudo systemctl restart httpd

Add the below line in the .htaccess file to activate the RewriteEngine.

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

Save the fine and exit from the vi editor.

RewriteEngine Conditions with Logic

RewriteCond is a directive used if a rewiteCond is enabled, then the Apache will follow the RewriteRule.

The apache will handle the request for an invalid web pace or URL it will display the 404 Page Not Found, instead of displaying the 404 error page, we can redirect the URL back to the home page or other web page.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/(.*)$ /home

IP address Restriction

We can use Rewite Condition to allow or block the traffic from a particular IP address.

For demo, below is the code where we want to block the traffic from all and accept the traffic from one address.

RewriteCond %{REMOTE_ADDR} !^(198\.168\.100\.20)$
RewriteRule (.*) - [F,L]

Options Used.

%{REMOTE_ADDR} - is the address string.
!^(198\.168\.100\.20)$ - the IP address where to allow the web traffic.
F – Flage deny the access
L – Which indicates the last rules to run.

In this above article, we have enabled the RewriteRules and RewriteCond on the Apache where we want to redirect the traffic with or without conditions and also, the mod_rewrite is an important directive where the Apache web server uses and redirects the web traffic to other site or other location with or without condition.

Published on 21-Jan-2020 14:19:30