• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Add or Remove WWW Prefix in Domain URL

Written by Rahul, Updated on November 9, 2017

The SEO specialist suggests running website either with www or without www domain only. You can automatically add or remote www prefix from domain’s URL on Apache and Nginx web server. Apache users can do this using .htaccess file with mod_rewrite module enabled. Nginx users can do this in Nginx server block settings. In this tutorial, you will get to add or remove www on Apache (using mod_rewrite) and Nginx web servers.

Remove WWW Prefix in Domain URL

Add the following code in your web server configuration. This will remove www from domain site URL. If someone opened the site with www, this will redirect to non-www URL.

Apache users add the following code in your website .htaccess file. This required the mod_rewrite enabled on your server, without mod_rewrite it will not work anymore.

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

Nginx users add the following code in domains server block.

server {
 server_name www.example.com;
   return 301 http://example.com$request_uri;
}

Adding WWW Prefix in Domain URL

Add the following code in your web server configuration. This will add www in domain site URL. If someone opened site without www, this will redirect to a domain with www URL.

Apache users add the following code in your website .htaccess file. Remember to enable mod_rewrite module.

  RewriteEngine On 
  RewriteCond %{HTTP_HOST} ^example.com$ 
  RewriteRule (.*) http://www.example.com$1 [R=301]

Nginx users add the following code in domains server block.

server {
 server_name example.com;
   return 301 http://www.example.com$request_uri;
}

References:
https://www.nginx.com/blog/creating-nginx-rewrite-rules/
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy