How to redirect www URLs to non-www URLs in website

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 {
    return 301 $scheme://$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} ^$ [NC]
RewriteRule ^(.*)$$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.