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]
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!
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]
-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.
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.