How to redirect www URLs to non-www

  374 views   11 months ago Ubuntu

When considering to SEO, if you are not redirecting www to non-www or vice-versa, then search engines like Google webmaster, Bing will create multiple URLs of same link.

You can redirect www to non-www URLs by server side language or through web server like apache or NginX server. In this article, we will go through how to redirect www URLs to non-www URLs through apache web server or NginX server.

NginX web server

NginX web server default configuration file stored at /etc/nginx/nginx.conf. You can put the below code in server key or create a new configuration file in your configuration directory, like /etc/nginx/conf.d/website.conf and place the following configuration options.

server {
    server_name www.website.com;
    return 301 $scheme://website.com$request_uri;
}

Apache web server

Apache is widely used in web application. Apache website custom configuration file .htaccess located at root directory of project. All you have to do is add the below code between <IfModule mod_rewrite.c> and </IfModule>.

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

If you have SSL installed for your domain, put https instead of http in RewriteRule

Thats it what you have to do. In the next article, we will also discuss how to redirect non-www URLs to www URLs. So if you want your website url with www, then it will be helpful to you.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]