Latest news from 5wire Networks

How To Redirect www to Non-www with Apache on CentOS 7

Introduction

When you have your web site or application up and running behind a domain, it is often desirable to also allow your users access to it via the plain domain name and the www subdomain. That is, they should be able to visit your domain with or without the “www.” prefix, e.g. example.com or www.example.com, in a web browser, and be presented with the same content. While there are a variety of ways to set this up, the best solution, for consistency and SEO considerations, is to choose which domain you prefer, plain or www, and redirect the other one to the preferred domain. This type of redirect is called a Permanent Redirect, or “301 redirect”, and can be easily set up by properly configuring your DNS resource records and web server software.

This tutorial will show you how to redirect a www URL to non-www, e.g. www.example.com to example.com, with Apache on CentOS 7. We will also show you how to redirect in the other direction, from a non-www URL to www.

If you want to perform this type of redirect with Nginx as your web server, you should follow this tutorial instead: How to Redirect www to non-www with Nginx on CentOS 7.

Prerequisites

This tutorial assumes that you have superuser privileges, i.e. sudo or root, on the server that is running Apache. If you don’t already have that set up, follow this tutorial: How to set up your new CentOS 7 server.

It is assumed that you have Apache (httpd) installed. You can see how to do that here.

You must be able to add records to the DNS that is managing your domain.

Let’s get started by configuring your DNS records.

Configure DNS Records

In order to set up the desired redirect, www.example.com to example.com or vice versa, you must have an A record for each name.

Open whatever you use to manage your DNS. If you use 5wire services, this can be done in cPanel.

If a domain (also known as a zone) record does not already exist, create one now. The hostname should be your domain, e.g. example.com, and the IP address should be set to the public IP address of your Apache server. This will automatically create an A record that points your domain to the IP address that you specified.

Log in to cPanel, you will see DNS Functions:

 

Go in to DNS Functions, click Edit DNS Zone.

You’ll be faced with something like this:

Locate the lines that display your www.example.com and example.com, then change their settings to IN A and point these to your IP.

Go to the bottom of the page, click save and load the changes.

Now your server should be accessible via the www and non-www-domain, but we’ll need to set up the redirect.

Enable Apache Rewrite Module

In order to perform the 301 redirect, we will use the Apache mod_rewrite, or Rewrite, module. Doing so will ensure that your users can access your site with or without the www. prefix, and be redirected to the domain that you prefer.

The Rewrite module, on CentOS 7, is enabled by default. If it isn’t enabled, for some reason, be sure to add this line to the modules section of your Apache configuration (which is in /etc/httpd/conf.modules.d/00-base.conf by default):

LoadModule rewrite_module modules/mod_rewrite.so

Enable .htaccess Files

We will configure Apache with redirect rules using .htaccess files. This is a feature that must be enabled by relaxing the DocumentRoot’s Directory permissions.

Open your Apache configuration file for editing. On CentOS, the default configuration file is located at /etc/httpd/conf/httpd.conf, so we will use that in our example:

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

Assuming that you are using the default DocumentRoot, /var/www/html, find the configuration block that corresponds to it, <directory “/var/www/html>. Within the block, change AllowOverride None to AllowOverride All:

<Directory /var/www/html>
...
    AllowOverride All
...
 </Directory>

Save and exit.

Now restart Apache to put the change into effect:

sudo systemctl restart httpd

Now Apache is configured to read .htaccess files located anywhere under the /var/www/html directory. Let’s add our Rewrite rules now.

Configure Rewrite Module

As we mentioned earlier, we will configure the Rewrite module using an .htaccess file.

Change directories to your DocumentRoot, in our case, /var/www/html:

cd /var/www/html

Now open .htaccess for editing (use nano or vi, any text editor you wish):

sudo vi .htaccess

If you haven’t created the file before, it will be blank. Depending on which direction you want to redirect, use one of the following options:

Option 1: Redirect www to non-www

If you want redirect users from www to a plain, non-www domain, insert this configuration:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Save and exit. The changes should go into effect immediately. Note that if you are using HTTPS, you should update “http“, in the RewriteRule line, to “https“.

Use this curl command to ensure that the non-www domain redirects to the www domain (replace the highlighted part with your actual domain):

curl -I http://www.example.com

You should get a 301 Moved Permanently response, that shows the non-www redirect location, like this:

HTTP/1.1 301 Moved Permanently
Date: Mon, 04 May 2015 16:04:56 GMT
Server: Apache/2.4.6 (CentOS)
Location: http://example.com/
Content-Type: text/html; charset=iso-8859-1

You should attempt access your domain in a web browser (www and non-www) to be sure it works.

Option 2: Redirect non-www to www

If you want redirect users from a plain, non-www domain to a www domain, insert this configuration:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Save and exit. the changes should go into effect immediately. Note that if you are using HTTPS, you should update “http”, in the RewriteRule line, to “https”.

Use this curl command to ensure that the non-www domain redirects to the www domain (replace the highlighted part with your actual domain):

curl -I http://example.com

You should get a 301 Moved Permanently response, that shows the www redirect location, like this:

HTTP/1.1 301 Moved Permanently
Date: Mon, 04 May 2015 16:05:26 GMT
Server: Apache/2.4.6 (CentOS)
Location: http://www.example.com/
Content-Type: text/html; charset=iso-8859-1

You should attempt access your domain in a web browser (www and non-www) to be sure it works.

Job done!

The Apache redirect is correctly configured, and your users will be able to access your web server via your non-www and www domain.

 

25% DISCOUNT FOR LIFE

Life time discount when you purchase your first order. Applies to Web Hosting, Reseller Hosting, and Cloud Servers.

Free Domain included on yearly Web Hosting plans.

Use code WEBOFF at the checkout!

Terms and Conditions apply. Voucher code is only valid for new customers.