How to Enable mod_rewrite for Apache on CentOS


how to enable mod rewrite in apache on centos

Sometimes, when you move a website to a different domain, or some of its pages to a different location, you’ll need to be able to redirect old URLs to new ones. It is extremely tedious to build this from scratch. However, if you are using Apache web server, then it provides a very useful module called mod_rewrite that allows you to easily setup URL redirects or even rewrite URLs before they are processed. Here’s how to enable mod_rewrite for Apache on CentOS.

 

How to Enable mod_rewrite for Apache on CentOS

You can enable mod_rewrite for Apache on CentOS using the following steps. Technically speaking, mod_rewrite is enabled by default on Apache in CentOS. So it’s just a matter of installing Apache properly and testing the set up.

 

1. Install Apache

We will install Apache using yum, the default package manager for CentOS.

$ sudo yum install httpd

You might see a prompt

Is this ok [y/d/N]:

Type ‘Y’ and press Enter, to complete the installation.

 

Next start Apache daemon, using the systemctl utility.

$ sudo systemctl start httpd

 

2. Verify mod_rewrite

Starting CentOS version 7, mod_rewrite is enabled, by default, during Apache installation. You can simply verify it with the command:

$ httpd -M

You’ll see a list of installed modules in your Apache server.


Output
. . .
remoteip_module (shared)
reqtimeout_module (shared)
 rewrite_module (shared)
setenvif_module (shared)
slotmem_plain_module (shared)

 

If you don’t see the rewrite_module, you can manually enable it by editing the 00-base.conf file in vi editor:

$ sudo vi /etc/httpd/conf.modules.d/00-base.conf

Once the editor opens, press ‘i’ to enter edit mode and add/edit the highlighted line below


#
# This file loads most of the modules included with the Apache HTTP
# Server itself.
#
. . .
LoadModule rewrite_module modules/mod_rewrite.so
. . .

 

Press ESC key to exit edit mode. Press :wq to save and exit the file.

 

Now restart Apache for the changes to take effect.

$ sudo systemctl restart httpd

 

3. Set up .htaccess file

.htaccess file is the heart of mod_rewrite module where you can define rewrite rules and redirects, without modifying any of the server configuration files.

Before we set up .htaccess file, we need to modify the server configuration to be able to overwrite Apache directives.

Open http.conf file using vi editor:

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

Look for <Directory /var/www/html> and change the directive AllowOverride 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>
. . .

Save and exit the file. Restart Apache to apply the changes


$ sudo systemctl restart httpd

 

Next, create a .htaccess file in Apache’s root folder (/var/www/html)


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

 

Add the following line at the top, save and exit the file.


RewriteEngine On

This tells Apache to process the rules that follow.

 

4. RewriteRule Syntax

RewriteRule directive allows you to set up URL rewrite rules. Its syntax is

RewriteRule Pattern Substitution [Flags]
  • RewriteRules – Server directive for defining rewrite rules
  • Pattern – A regular expression that matches the desired string in requested URL
  • Substitution – Where the requested URL should be sent for processing
  • [Flags] – optional parameter to modify the rule. Here’s the detailed documentation about Rewrite Flags.

.htaccess can have more than one rewrite rules. However, Apache will execute them in the defined order only. When Apache gets a URL that matches one of the rewrite rules, it will remap that URL to the destination URL.

 

5. RewriteCond Syntax

RewriteCond allows us to add conditions to rewrite rules. Its syntax is

RewriteCond TestString Condition [Flags]
  • RewriteCond – Server directive for defining conditions
  • TestString – String to test against
  • Condition – pattern for matching
  • [Flags] – optional parameter to modify condition

RewriteCond will allow Apache to process any RewriteRule that follow it, only if the condition is true.

 

6. Setting Up Rewrite Rules and other files

Let us setup a simple rewrite rule that redirects visitors from www.your_domain.com/about.html to www.your_domain.com/about without having to type the extension (.html) in URL. Here www.your_domain.com is your website’s domain.

Start by creating an about.html file in vi editor


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

Paste the following HTML in it. Save and exit the file.


<!DOCTYPE html>
<html>
 <head>
  <title>About Us</title>
 </head>
 <body>
  <h1>About Us</h1>
 </body>
</html>

 

Open your web browser and go to:


http://www.your_domain.com/about.html

 

You’ll see the contents of ‘About Us’ page on it. Now if you remove .html from the URL and visit


http://www.your_domain.com/about

 

You’ll see a 404 Page not found error. That is because, by default, Apache looks exactly for the URL you have typed and not any of its variations. You can change this using rewrite rules.

So we open our .htaccess file

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

 

Now comes the most important step to enable mod_rewrite for Apache on CentOS. After the first line, you can add:

RewriteRule ^about$ about.html [NC]

 

Now your .htaccess file will look like

RewriteEngine on
RewriteRule ^about$ about.html [NC]

 

In the above statement, ^about$ is matched in the URL. ^ is a metacharacter that indicates start of the URL after the domain name ends, and $ indicates the end. So the above statement means that when a user types a url your_domain.com/about he/she should be served the contents of your_domain.com/about.html.

[NC] tells Apache to ignore case of URL. So all the following URLs will point to about.html:

  • your_domain.com/about
  • your_domain.com/About
  • your_domain.com/about.html

But the following will not:

  • your_domain.com/about/
  • your_domain.com/product

 

Now, restart Apache

$ sudo service apache2 restart

 

Congratulations! You have enabled mod_rewrite for Apache on CentOS. Your users can now access your_domain.com/about

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!