How To Add or Remove WWW Prefix in Domain URL

How To Add or Remove WWW Prefix in Domain URL

The SEO specialist suggest to run 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 some one opened 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 some one opened site without www, this will redirect to 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:

Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!

 
Enjoyed this video?
How To Add or Remove WWW Prefix in Domain URL
"No Thanks. Please Close This Box!"