Remove and add www from url using apache .htaccess file

Having www in URL of your webpages is not a necessary. It is matter of choice. Most of the internet users are still typing www in from of every domain they enter into they web browser. By use of apache’s .htaccess file yuo can force your URL to contain www or you can remove www completely. Here is a way how to add www to your URL. Edit your .htaccess file:

Add www to URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$
RewriteRule (.*)$1 [R=301]

Remove www from URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

In both cases replace domain name with your website’s domain name.

When you are done make sure that you test both URL versions. Both of them should work but only one will be rewritten.