Redirect non-www to www URLs using htaccess


Web development has a lot of nuances that are learned along the way. One of those is that www and non-www URLs are technically different and that it could hurt your SEO if you keep both around.

For example, the domains http://www.7lessons.info and http://7lessons.info (note the "www" in one but not the other) are technically different. However, if you click on either, they'll redirect to the same place, which is a good thing. It helps Google and other search engines to only index one and to not split results.

This tutorial will show you how to redirect your non-www URLs to www, or vice-versa, using htaccess. Let's get started.

Which one should I choose?
You have the choice - it doesn't really matter if you choose to redirect non-www to www or vice versa. It's a stylistic choice that comes down to whichever you prefer.
Traditionally, sites have opted to redirect to the www version. When you visit sites like Google, Facebook, YouTube, Yahoo, Wikipedia, and most other large sites, they redirect to the www version. Many of us are used to saying "www dot" when we tell someone a new website that they should visit and we're used to seeing the "www" in the domain after landing on the site.
www is as ubiquitous as the web itself. And, obviously, it stands for "world wide web" and was namespaced as such.
Thus, most sites have opted to use the traditional "www" in their domain. That way when non-techy users see the "www" in the browser address, they know they're at the "right" place.
However, it doesn't have to be that way. Some newer sites are starting to use the non-www version. They either like the way it looks or just want to be a bit different and stand out.
In the end, it's very minor in the grand scheme of things. People are concerned about your site and its content, rather than whether your domain has a www in it or not. So go with whatever you'd like!

On an Apache server
Most users reading this article will likely be on an Apache server. Here are the steps you'll need.
Use your host's file manager or an FTP program to access your .htaccess file. Note that it has a dot preceding it.
If you don't find, check to see if there's a htaccess.txt file, which can be renamed to .htaccess if it came with your CMS (edit the file and check its contents to be sure). If there isn't a htaccess.txt file either, create a new file called .htaccess.
Then insert either of the following lines and modify the code with your domain:
Redirect non-www to www

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

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

Redirect www to non-www

RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]

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

If you had a "RewriteEngine On" in your htaccess file already, insert the above code just after the line reading: "RewriteEngine On".

Afterward, check your site (including some internal pages) and try different www and non-www combinations of your domain to see if the redirect is working as expected.

On an IIS server

On a Windows (IIS) server, find your web.config file and make the following rule changes to your rules tag:

Redirect non-www to www

 <configuration> 

 <rewrite>

 <rules>

 <rule name="Redirect to www" stopProcessing="true">

 <match url=".*" />

 <conditions>

 <add input="{HTTP_HOST}" pattern="^yourdomain.com$" />

 conditions>

 <action type="Redirect" url="http://www.yourdomain.com/{R:0}"

 redirectType="Permanent" />

 <rule>

 <rules>

 <rewrite> 

 <configuration> 

Redirect www to non-www

 <configuration> 

 <rewrite>

 <rules>

 <rule name="Redirect to non-www" stopProcessing="true">

 <match url=".*" >

 <conditions>

 <add input="{HTTP_HOST}" pattern="^yourdomain\.com$" />

 conditions>

 <action type="Redirect" url="http://yourdomain.com/{R:0}" redirectType="Permanent" />

 <rule>

 <rules>

 <rewrite> 

 <configuration>


Afterward, check your site, including some internal pages, and try different www and non-www combinations of your domain to see if the redirect is working as expected.

Social Share

Leave a Reply