≡ Menu

How to redirect non-www to www with htaccess

301 Redirect

Here’s a short tutorial to permanently (301) redirect non-www website address, such as mydomain.com, to www.mydomain.com. We will edit the .htaccess file on the web server. This method is applicable to Apache web server running on Linux.

Here’s the 4 lines of codes to be added to the .htaccess file:
RewriteCond %{HTTP_HOST} !^www\..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*)\.(com)
RewriteRule ^.*$ http://www.%1.%2%{REQUEST_URI} [R=permanent,L]

You will need access to the CPanel of your web server or use a FTP client e.g. Filezilla to upload a text file.

CPanel File Manager

For me, it’s quicker to log in to my web server using CPanel and edit the .htaccess with the File Manager.

non-www to www htaccess codes

The above codes will work for .com domain. If you’re using other types of domain, e.g. .net or .org, then replace .(com) with .(net) and .(org) respectively. Alternatively, you may modify .(com) to .(com|net|org) to cater for these 3 types of domain.

{ 0 comments… add one }

Leave a Comment