• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to configure redirect rules for Non-www to www redirect, and www to non-www redirect on ubuntu 22.04

  • 00:57 lsb_release -a
  • 01:13 systemctl status apache2
  • 01:26 a2enmod rewrite
  • 01:40 vim /etc/apache2/sites-enabled/000-default.conf
  • 03:14 systemctl restart apache2
  • 03:36 cd /var/www/html/
  • 03:45 vim .htaccess
  • 06:01 curl -I //www.linuxhelp.com
  • 06:42 vim .htaccess
  • 08:46 curl -I //linuxhelp.com
{{postValue.id}}

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 snap1

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.

snap2

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.

Tags:
matthew
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What are Apache rewrite rules?

A

Apache rewrite rules are configurations used in the Apache HTTP Server to modify or redirect incoming request URLs. They are defined in the .htaccess file or in the server configuration files (e.g., httpd.conf) using the mod_rewrite module. These rules enable URL manipulation, redirection, and rewriting.

Q

How do I enable mod_rewrite?

A

To enable mod_rewrite:
• Open the Apache configuration file (httpd.conf or apache2.conf).
• Ensure the line LoadModule rewrite_module modules/mod_rewrite.so is uncommented.
• Restart Apache to apply the changes (sudo systemctl restart apache2 or sudo service httpd restart).

Q

What is the basic syntax of a rewrite rule?

A

The basic syntax of a rewrite rule is:
RewriteRule Pattern Substitution [Flags]
• Pattern: The regular expression to match the incoming URL.
• Substitution: The URL to which the request should be rewritten or redirected.
• Flags: Optional parameters to control the behavior of the rule (e.g., [L] for "last rule").

Q

How do I redirect an old URL to a new URL?

A

To redirect an old URL to a new URL:
RewriteEngine On
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
• R=301 specifies a permanent redirect.
• L means this is the last rule to be processed if the rule matches.

Q

How do I rewrite a URL to a different path internally?

A

To internally rewrite a URL without changing the visible URL:
RewriteEngine On
RewriteRule ^old-path/(.*)$ /new-path/$1 [L]
• This rewrites old-path/something to new-path/something internally.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Lucas ?
Various options in Top command

Am using Top command only to view the load average, what are the various options in Top command..??

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.