Steps to WWW and Non-WWW Redirection in Apache on CentOS 7

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
www and non-www redirection in apache

Quick Guide to Sove WWW and Non-WWW redirection in Apache on CentOS 7

There are many diverse problems while managing a website or application on the Internet. It’s up to you recognizing them as critical as possible while developing a website. Out of these knocks of website development, we found a common issue and it is www and non-www format of a website. Choosing the perfect prefix is the core decision because ultimately, the format was going to be your brand name in the future.




However, the main concern is that you need the mechanism to get it worked whether a user wants to access a website through www or without www. It should be worked when a user accesses a global form of a website like www.yourwebsite.com. In case, if a user tries to browse a website with the online domain name using dot com extension then it should work for them.

In order to solve this critical www and non-www redirection issue, we built the easy guide to aid website owners and developers and we are sure that the steps will defiantly resolve the issue of www and non-www redirection issue.

We want to cater a specific series of steps to solve an issue of www and non-www redirection in Apache on CentOS 7.

Let’s get into it!

Before we jump into the process, first we should configure DNS record through public IP address.

And, in order to do that, you should add two different entries such as “@” with public IP address and then the second one is “www” with the same public IP address.

Once you did with DNS configuration, you need to move further to configure Apache Rewrite Module.

It’s critical step to solve this issue.

Yes, the advantage of using CentOS 7 is that it enables default way that Apache Rewrite Module.

Somehow, you are not sure that the Apache Rewrite Module isn’t enabled then you should copy & paste below like of code in Apache Configuration.

Path of Apache Configure File; /etc/httpd/conf.modules.d/00-base.conf
Code: LoadModule rewrite_module modules/mod_rewrite.so

Okay, there are another requirement to move further in process and it’s quick to solve.

Yes, here are the steps to enable .htaccess file in CentOS 7.

Open default configuration file (you could browse through below location).

Path; /etc/httpd/conf/httpd.conf

Now, discover the following code; “AllowOverride None

Change above code status with “AllowOverride All

Now save the changes and exit from that path.

Restart Apache to have the following changes in action.

We are almost done, but still, there is the last step to have action.

The configuration of Rewrite Module through the .htaccess file.

Browse a htaccess file from root directory; /var/www/html

Right click on a htaccess file and choose the option “Edit”.

Now, it’s time for the big decision whether you want to go with www or without www. One more time, I would repeat the same word that how would like to represent your business to users it’s up to you.

It’s okay, we will give you both the situation code to solve www and non-www redirection.

Solution 1: Htaccess code for www to non-www domain name redirection

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

Copy and Paste above code in a htaccess file. Save it and exit. That’s it. You are done with www to non-www redirection issue.

You could cross check this change through 301 redirection checker tool.

Solution 2: Htaccess code for non-www to www domain name redirection

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

Copy and paste above code in a htacess file. Save it and exit. That’s it. You are made it one more time with non-www to www domain name redirection.

 

Leave a Reply

Your email address will not be published. Required fields are marked *