Redirect non-www to www URLs using htaccess

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 https://www.ostraining.com and https://ostraining.com (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>
<system.webServer>
<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>
</system.webServer>
</configuration>

Redirect www to non-www

<configuration>
<system.webServer>
<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>
</system.webServer>
</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.

Author

  • Nick Savov

    Nick is the Director of Support at OSTraining and you can find him in almost every area of the site, from answering support requests and account questions to creating tutorials and software.

0 0 votes
Article Rating
Subscribe
Notify of
33 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Yaeger Design
Yaeger Design
8 years ago

I always use:

RewriteCond %{HTTP_HOST} !^yaegerdesign\.net$ [NC]

RewriteRule ^(.*)$ [url=http://yaegerdesign.net/$1]http://yaegerdesign.net/$1[/url] [L,R=301]
cPanel always generates:

RewriteCond %{HTTP_HOST} ^www\.yaegerdesign\.net$

RewriteRule ^/?$ “http\:\/\/yaegerdesign\.net\/” [R=301,L]
The first method is harder to manage if you use a lot of sub-domains, but can be a better catch-all if someone uses www1, www2, or any sort of misspelling of the sub-domains.
Which do you prefer?

Todd Zillah
Todd Zillah
7 years ago
Reply to  Yaeger Design

I rarely reply to posts, but THANK YOU!! None of the free Joomla canonical extensions work, and the one that does is $33.
this works perfect!
RewriteCond %{HTTP_HOST} !^yaegerdesign\.net$ [NC]
RewriteRule ^(.*)$ [url=http://yaegerdesign.net/$1]http://yaegerdesign.net/$1[/url] [L,R=301]

Yaeger Design
Yaeger Design
7 years ago
Reply to  Todd Zillah

You’re welcome!

mrjosephcampbell
8 years ago

For those using Joomla! you can use the Yireo SSL Redirection plugin:

[url=http://extensions.joomla.org/extension/yireo-ssl-redirection]http://extensions.joomla.or…[/url]
It offers a number of parameters that makes direction super easy.

Evan Baldonado
Evan Baldonado
8 years ago

It’s not working for me 🙁
excerpt from .htaccess file of [url=http://aquariumkids.com]aquariumkids.com[/url]:
RewriteEngine On

RewriteCond %{HTTP_HOST} ^[url=http://www.aquariumkids.com]www.aquariumkids.com[/url] [NC]

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

RewriteEngine Off

manumm
manumm
8 years ago

Alternatively you can use the following code in your .htaccess file

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

I prefer to use www in my site – [url=http://www.cheenachatti.com/]http://www.cheenachatti.com/[/url]

Atul kumar
Atul kumar
5 years ago
Reply to  manumm

[quote]yes working fine congrats[/quote]

samisham
samisham
8 years ago

What do you do if it doesn’t work as expected? I have added the redirect to the .htaccess file and it is still picking up the non-www..?

Rommie Carnevale
Rommie Carnevale
7 years ago

Thanks so much for this post and code provided. I spent about and hour and a half searching for one that was clear enough to make me comfortable with the edits. Many of the other posts I read either only mentioned the code or the process. This post was straight to the post was straight to the point and covered both. This worked great. I even understand the .htaccess file better now from digging in. Thanks again! [url=http://rcreativeagency.com]rcreativeagency.com[/url] is now [url=http://www.rccreativeagency.com]www.rccreativeagency.com[/url] thanks to your help!

murugavel
murugavel
7 years ago

working like a charm

Distributor Seo
Distributor Seo
7 years ago

Thanks

Asp Team Dci
Asp Team Dci
7 years ago

Non-www to www redirection is not working for some cases ?
Redirect rule in .htaccess file in public_html folder for non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[url=http://yourdomain.com]yourdomain.com[/url] [NC]
RewriteRule ^(.*)$ [url=http:/www.domain.com/$1]http:/www.domain.com/$1[/url] [L,R=301]
Non-WWW Not Working URL:
[url=http://domain.com/http:/www.domain.com/images/bike.jpg]http://domain.com/http:/www…[/url]
I added customErrors in the config file. The above URL should go to custom 404 page. But this is not happened and shows Server Error in ‘/’ Application.
Error Screen shot attached
But below non-www URL works fine.
Non-WWW Working URL:
[url=http://domain.com/http/www.domain.com/images/bike.jpg]http://domain.com/http/www….[/url]
If I add WWW, it works fine.
How can I solve this?
WWW Working URL: [url=http://www.domain.com/http:/www.domain.com/images/bike.jpg]http://www.domain.com/http:…[/url]

Sagar Upadhayay
Sagar Upadhayay
7 years ago

Thanks nick

i use this code but my website not redirect www why what’s the problem

before rewrite engine on i use some codes for enable files catching script in htaccess

how to fix this problem what can i do

[url=http://wallpapersfestival.com]wallpapersfestival.com[/url] to [url=http://www.wallpapersfestival.com]www.wallpapersfestival.com[/url] i redirect all urls in non-www to www

but using this same code my website not work with it

david
david
7 years ago

hi there

id like to use this code in my web.config file but I run a multstore site – is there some code i can add to the web.config file like that wil redirect the different country level domains all from – www from non www ?
my multistore site runs various domain names..
i.e. [url=http://store.fr]store.fr[/url] / [url=http://store1.es]store1.es[/url] / [url=http://store2.ie]store2.ie[/url]. / [url=http://store3.nl]store3.nl[/url] .
Thanks !

Ryan Rajpoot
Ryan Rajpoot
7 years ago

I intended to redirect non-www to www and http to https. For that I have used below code and it’s resolving perfectly. Please check whether code is correct or not.
RewriteEngine On

RewriteBase /

#Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Redirect to HTTPS

RewriteCond %{HTTPS} off

RewriteRule (.*) [url=https://www.pnrstatusbuzz.in/]https://www.pnrstatusbuzz.in/[/url] %{REQUEST_URI}
Thank You.

masoud
masoud
7 years ago

hello

this code in my site not worked
this is my code in web.config

Shovan
Shovan
7 years ago

This is what I am trying to achieve
1. Website should always require www – Works

2. All 404 errors should redirect to home page – Works

3. On mobile website should redirect to [url=http://m.website.com]m.website.com[/url] – Not working
RewriteEngine On

# require www

RewriteCond %{HTTP_HOST} ^[url=http://website.com]website.com[/url] [NC]

RewriteRule (.*) [url=http://www.website.com/$1]http://www.website.com/$1[/url] [L,R=301]
# Redirect broken link to home

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule . / [L,R=301]
# Mobile redirect

RewriteCond %{HTTP_USER_AGENT} android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\\ |maemo|midp|mmp|opera\\ m(ob|in)i|palm(\\ os)?|phone|p(ixi|re)\\/|plucker|pocket|psp|symbian|treo|up\\.(browser|link)|vodafone|wap|windows\\ (ce|phone)|xda|xiino [NC,OR]

RewriteCond %{HTTP_USER_AGENT} ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\\ wa|abac|ac(er|oo|s\\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\\-m|r\\ |s\\ )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\\-(n|u)|c55\\/|capi|ccwa|cdm\\-|cell|chtm|cldc|cmd\\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\\-s|devi|dica|dmob|do(c|p)o|ds(12|\\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\\-|_)|g1\\ u|g560|gene|gf\\-5|g\\-mo|go(\\.w|od)|gr(ad|un)|haie|hcit|hd\\-(m|p|t)|hei\\-|hi(pt|ta)|hp(\\ i|ip)|hs\\-c|ht(c(\\-|\\ |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\\-(20|go|ma)|i230|iac(\\ |\\-|\\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt(\\ |\\/)|klon|kpt\\ |kwc\\-|kyo(c|k)|le(no|xi)|lg(\\ g|\\/(k|l|u)|50|54|e\\-|e\\/|\\-[a-w])|libw|lynx|m1\\-w|m3ga|m50\\/|ma(te|ui|xo)|mc(01|21|ca)|m\\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\\-|\\ |o|v)|zz)|mt(50|p1|v\\ )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\\-2|po(ck|rt|se)|prox|psio|pt\\-g|qa\\-a|qc(07|12|21|32|60|\\-[2-7]|i\\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\\-|oo|p\\-)|sdk\\/|se(c(\\-|0|1)|47|mc|nd|ri)|sgh\\-|shar|sie(\\-|m)|sk\\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\\-|v\\-|v\\ )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\\-|tdg\\-|tel(i|m)|tim\\-|t\\-mo|to(pl|sh)|ts(70|m\\-|m3|m5)|tx\\-9|up(\\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\\-|\\ )|webc|whit|wi(g\\ |nc|nw)|wmlb|wonu|x700|xda(\\-|2|g)|yas\\-|your|zeto|zte\\-) [NC]
RewriteRule ^$ [url=http://m.website.com]http://m.website.com[/url] [L,R=302]
On the mobile site – [url=http://m.website.com]m.website.com[/url]

redirected you too many times.

ERR_TOO_MANY_REDIRECTS
Can you help me fix this?
Thanks

Chad Moore
Chad Moore
7 years ago

how can i redirect my website in every instance of url such as if some typed [url=http://yourdomain.com]yourdomain.com[/url] ,[url=http://www.yourdomain.com]www.yourdomain.com[/url] [url=http://yourdomain.com]http://yourdomain.com[/url] ,[url=http://www.yourdomain.com]http://www.yourdomain.com[/url] ,[url=https://yourdomain.com]https://yourdomain.com[/url] or [url=https://www.yourdomain.com]https://www.yourdomain.com[/url] is this possible to have them all redirect to [url=https://www.yourdomain.com?]https://www.yourdomain.com?[/url]

daniel-pickering
7 years ago
Reply to  Chad Moore

Hi Chad yes you can use htaccess to redirect all of those to a URL of your choosing. The specifics of the condition will depend on your server setup.
Thanks

Daniel

Jeena
Jeena
6 years ago

I used redirect code for non www version to www verison in apache server.Now i am planning to move http to https .i want to redirect code for http to https without affecting the seo ranking.

Laxmikant S Bhumkar
Laxmikant S Bhumkar
6 years ago

Thanks, Nick sir. Good interpretation of the logic behind using/non-using of WWW.

isha
isha
5 years ago

Hello i have redirect my website www to non www By this code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.resortsneardelhincr\.in [NC]
RewriteRule ^(.*)$ http://resortsneardelhincr.in/$1 [L,R=301]

But after redirect in webmaster tool total links are not showing properly

http://resortsneardelhincr.in/

JOPESTKIL KENYA
JOPESTKIL KENYA
4 years ago

Nice post. Thanks so much for this post and code provided which made my website to have perfect solution. I spent about and hour and a half searching for one that was clear enough to make me comfortable with the edits. Many of the other posts I read either only mentioned the code or the process. I love to learn from this post for more corrections. 

v.Hemalatha
v.Hemalatha
4 years ago

Yoast SEO is a WordPress plug-in designed to help you improve some of the most important on-page SEO factors–even if you aren’t experienced with Web development and SEO. This plug-in takes care of everything from setting up your meta titles and descriptions to creating a sitemap. Yoast even helps you tackle the more complex tasks like editing your robots.txt and .htaccess.

Some of the settings may seem a little complex if you’re new to SEO and WordPress, but Yoast created a complete tutorial to help you get everything set up. And the team at WPBeginner made this handy video to help you get set up quickly.

sanju samson
sanju samson
3 years ago

Hello, I am not sure will my site be affected if I make changes in my htaccess file. Thank you for helping out 🙂

Matthew Carpenter
Matthew Carpenter
3 years ago

There is a reason to stick with the ‘www’ in your URLs, and it’s not just a question of aesthetics. This is going to make a difference if you plan to use a CDN like CloudFlare. CloudFlare currently won’t work with non www sites.

I’ve been working through a process of changing my site from https:// to https://www to address this issue, and have found that this has caused some significant challenges in the updating of links written into posts, tags, pages, categories, etc. (there are some simple bulk edit plugins that will help you add the ‘www’ where it didn’t exist before).

But it isn’t just about those changes made on your site. Don’t forget your backlinks. And don’t forget any images that may have been embedded or linked elsewhere (also consider Google Image Search – do you really want to lose those results?).

What I’m currently tweaking is the .htaccess info for image redirects to include the ‘www’ version. Most of these have been fine by adding a simple line like this:
[code]Redirect 301 /wp-content/uploads/2020/01/How-to-Spot-Fake-Sea-Moss-with-4-Tips-on-Buying-The-Real-Deal-purple-sea-moss.jpgcomment image[/code]
There has been one image out of all of the ones changed that I’ve seen so far that stumped me. This was an image in the comments section of a post. I’ve temporarily taken the Redirect instruction out of the .htaccess file for this until I can figure out why it shows as a broken image link.

What brought me here was partially that, and seeking some clarification about having too many 301 Redirects in the .htaccess file. I’m aware that the sequencing of the information in the file is important to avoid looping, but as a bit of a noob I’m still searching.

daksh
daksh
3 years ago

where do I need to place the .htaccess file?

mike
3 years ago
Reply to  daksh

Typically, the root document for your site. Depending on where you host, your site’s files are commonly found in www, web, or http. If you’re connecting via ftp, you might automatically login to your site’s root document.

Mike
Mike
3 years ago

Hi Nick Thank you very much for sharing this post

I am using https version site , may i know should i change the below lines into 

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC] change to [size=3] [color=#000000]{[b]HTTPS_HOST[/b]}[/color] [/size]and paste in .htacess

please guide me

33
0
Would love your thoughts, please comment.x
()
x