Htaccess For No WWW

Sitepoint Members,

I searched sitepoint before asking figuring this probably was asked before, but the search function didn’t find anything.

I searched online for htaccess code such that whether a visitor types www or not while trying to get to my site, when the vistor does arrive at my site there’s no www in the web address of my site in the browser address bar. Also, I want goofle to see my site as no www. Here’s some of the code I found:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]


#nearly universal www – (removes WWW from the URL on both HTTP and HTTPS)

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.)$ [NC]
RewriteRule ^(.
)$ http://%1/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.)$ [NC]
RewriteRule ^(.
)$ https://%1/$1 [R=301,L]


Replace ‘domain.com’ with your domain name

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

What’s best?

Thanks,

Chris

A combination of 1 and 3. 2 is stupid, because you don’t need to make the distinction between http and https, if you just put the rules there it doesn’t matter whether http or https was requested.

1 uses (.*) , which is bad. 3 uses the correct version, i.e., .? and %{REQUEST_URI}, but does something really weird with the domain.

So


RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site\\.com$ [NC]
RewriteRule .? http://your-site.com%{REQUEST_URI} [L,R=301]

Where you need to replace your-site.com with your domain. And don’t forget to escape dots in the RewriteCond! :slight_smile:

Scallio,
Thanks for the help! When you write
“don’t forget to escape dots in the RewriteCond!”

you mean don’t forget the $ after “.com”, right?

Thanks,

Chris

escape the dots means

when you’re matching a real . in a pattern (like foo.com), do foo\.com


RewriteCond %{HTTP_HOST} !^your-site[color=#ff0000][b]\\.[/b][/color]com$ [NC]

do this only in the pattern part, not the part being redirected to.

I see. Thanks Stomme

That code works so good it stopped an error PageSpeed found that was coming and going:
Enable Keep-alive.
The old code was
RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-site.com
RewriteRule ^(.*)$ http://www.your-site.com/$1 [r=301,L]

Thanks again,

Chris

-second-
ScallioXTX, Stomme,
I noticed both google and sitepoint use www in their address. Should a small website use the www? I read that ‘www’ is obsolete and gets in the way of keywords in addresses.

Thanks,

Chris

It’s personal taste really. I doesn’t matter whether you use www or not, certainly not for your rankings.

ScallioXTX,

I guess I’ll levae it for now. Would the code then be

RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site\.com$ [NC]
RewriteRule .? [noparse]http://www[/noparse].your-site.com%{REQUEST_URI} [L,R=301]

Thanks,

Chris

Yes :slight_smile:

Thanks for the help Scallio !

Scallio,
Firefox says the the page is being redirected in a way that would never complete. I checked it carefully. It’s on a webhost server, if it matters.

Thanks,

Chris

Scallio,
Got it. Put www on both lines. No www typed into browser gets directed to www.

Thanks,

Chris