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.

Advertisement

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.
Leave A Reply

Exit mobile version