htaccessWeb Developer

Force www or non-www in Your Website’s URL with .htaccess

Written by:

Every noticed how when you open some websites, the URL will automatically add the “www” in the URL? Well that is done with a few simple lines of code in your website’s HTACCESS file. Let’s see how!


You might be thinking “what the heck is htaccess?” Simply put, it’s a configuration file that lets your web server know some customizable settings for your website. Almost every website has a HTACCESS file, even though you may not realize it. Some of the more common uses for HTACCESS is getting custom error pages for your website, hiding the file extensions, perform redirects, and you guessed it force www into your websites address.

How to Setup Your HTACCESS file

Like I said earlier, most websites already have a HTACCESS file. So if yours already does, you can go ahead and skip to the next part. If not then open up your favorite text editor, mine is Notepad++. Create a new file and add the following code into it:

<Files .htaccess> 
order allow,deny
deny from all 

RewriteEngine on

This is the bare minimum code you will need in your HTACCESS file. The first 4 lines are pretty standard. They make it so anyone who tried to open you HTACCESS file from their web browser or try to download it from another means (other than you using your FTP client) will not be able to view the code. The RewriteEngine setting is also pretty straight forward. It tell the web server to allow HTACCESS to be able to manipulate the URLs for your website. Easy enough.

Check this out:  Introduction to phpMyAdmin | What is phpMyAdmin?

Forcing WWW on Your Site

Now for where the real magic happens. Below is the code that automatically forces the www into any of address for your website when a user opens your site.

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

Simply change the domain name and the domain extensions as you need to for your own website and then like magic, your website will always have the www in it. No matter what page your users are browsing to. Be careful to keep all of the miscellaneous symbols like the forward slashes, periods and even the dollar signs in their very specific locations. Changing any of these may lead to your web site not opening or giving you a 501 server error.

Forcing non-WWW on Your Site

If you want the opposite effect, making your site NEVER have www in the address. the code is very similar.

RewriteCond %{HTTP_HOST} ^www\.heytuts\.com [NC]
RewriteRule ^(.*)$$1 [L,R=301]

Saving Your Work

Now take your seven lines of code and save them into the ROOT of your website as “.HTACCESS”. Be very careful to make sure you don’t forget the period at the beginning of the file extension. Depending on the text editor you are using, you may need to set the “File Type” to be “All Types (*.*)”.

Now give your website a load and make sure the new HTACCESS file works. If you get a 501 error it is because there is either a typo in your code or you web host may not allow HTACCESS by default. If that is the case, contact your support or take a look though some of the FAQs.

One Reply to “Force www or non-www in Your Website’s URL with .htaccess”

  1. Deangelo Kinkin says:

    I need to to thank you for this very good read!! I absolutely enjoyed every bit of it. I have you book marked to look at new things you post…

Leave a Reply

%d bloggers like this: