How to configure redirect rules for Non-www to www redirect, and www to non-www redirect on ubuntu 22.04
To Configure Redirect Rules For Non-www To Www Redirect, And Www To Non-www Redirect On Ubuntu 22.04
Introduction:
When your website or application is operational under a domain, it is often beneficial to provide users with access through both the primary domain name and the "www" subdomain. This means that users should be able to navigate to your domain with or without the "www." prefix, such as example.com or www.example.com, and be presented with identical content.
Procedure
Step 1: Check the OS version
root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
Step 2: Check the status of the Apache service by using following command
root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024-11-23 02:30:18 IST; 27s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 9211 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 9216 (apache2)
Tasks: 55 (limit: 4551)
Memory: 5.7M
CPU: 33ms
CGroup: /system.slice/apache2.service
├─9216 /usr/sbin/apache2 -k start
├─9217 /usr/sbin/apache2 -k start
└─9218 /usr/sbin/apache2 -k start
Nov 23 02:30:18 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Nov 23 02:30:18 linuxhelp apachectl[9215]: AH00558: apache2: Could not reliably determine the >
Nov 23 02:30:18 linuxhelp systemd[1]: Started The Apache HTTP Server.
Step 3: Now enable rewrite module by using following command
root@linuxhelp:~# a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2
Step 4: Then I’m going to edit Apache default virtual host file
root@linuxhelp:~# vim /etc/apache2/sites-enabled/000-default.conf
Add the following lines
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Step 5: Restart Apache service by using following command
root@linuxhelp:~# systemctl restart apache2
Step 6: Change the directory, to Apache default document root directory.
root@linuxhelp:~# cd /var/www/html/
Step 7: Create Dot htaccess file by using vim editor.
root@linuxhelp:/var/www/html# vim .htaccess
Add the following lines for Redirect www to non-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Step 8: Curl the URL of your domain with www. Here, see it will redirect with out www
root@linuxhelp:/var/www/html# curl -I //www.linuxhelp.com
HTTP/1.1 301 Moved Permanently
Date: Fri, 22 Nov 2024 21:08:35 GMT
Server: Apache/2.4.52 (Ubuntu)
Location: //linuxhelp.com
Content-Type: text/html; charset=iso-8859-1
Step 9: Go to the browser and search with http://www.example.com. It will redirected to non www.domain

Step 10: Edit Dot H’T’access file
root@linuxhelp:/var/www/html# vim .htaccess
Remove the old lines and add the following lines
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Step 11: Curl the URL of your domain, It will redirect with www
root@linuxhelp:/var/www/html# curl -I //linuxhelp.com
HTTP/1.1 301 Moved Permanently
Date: Fri, 22 Nov 2024 21:12:33 GMT
Server: Apache/2.4.52 (Ubuntu)
Location: //www.linuxhelp.com/
Content-Type: text/html; charset=iso-8859-1
Step 12: Also go to the browser and search with your domain. It will redirected to www domain.

Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps to configure redirect rules for Non-www to www redirect, and www to non-www redirect on Ubuntu 22.04. Your feedback is much welcome.
Comments ( 0 )
No comments available