Redirect non-www URLs to www URLs using a .htaccess file

This article will walk you through how to direct non-www traffic to the www prefix on a Linux server using .htaccess files.

  1. Create a notepad file and save it as .htaccess if you do not already have an existing one.
  2. Update the .htaccess file with the following code and save; be sure to replace domain.com with your domain name.
  3. RewriteEngine On

    RewriteCond %{HTTP_HOST} ^domain.com [NC]

    RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

  4. Upload the .htaccess file via ftp to the site root and now your traffic will be redirected to www.domain.com.