How to Insert / Remove “www” From a URL

This requires your website to be hosted on an Apache server with mod_rewrite.

  1. Go to your website’s root directory (e.g., public_html).
  2. Open the .htaccess file. ┬áIf the file doesn’t exist, create it.
  3. To insert “www”:
  4. RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteRule (.*) http://www.example.com$1 [R=301]

  5. To remove “www”:
  6. RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

  7. Save and exit.