Loading

post

Getting Rid of the WWW for WordPress Multi-Sites

I had a brainwave today. A good’n. More like a tsunami than a brainwave. For a couple of weeks I’ve been figuring ways to ensure the URLs for all my sites are written with the www hostname dropped. This should be an easy task. In most cases we can just do something like:

RewriteCond %{HTTP_HOST} ^http://www\.example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example.com [NC]
RewriteCond %{HTTP_HOST} !^example.com [NC]
RewriteRule ^(.*) http://example\.com/$1 [L,R=301]

Which tells the server to look for URL requests beginning http://www.domain.tld or URL requests beginning www.domain.tld and to rewrite them as http://domain.tld without the www.

Works great… except when you have subdomains.

For example, with the above directives at the top of .htaccess, when the server sees a request for http://www.example.com it will rewrite the request to http://example.com. But when the server receives a request for http://www.bonus.example.com the server will not rewrite the request to remove the www. It won’t do it because the hostname “bonus” is stuck between www. and example.com.

The way to drop the www when working with WordPress multi-sites is — and I could have smacked myself on the head when I realized this; in fact, I did; and it hurt — the solution is to be a little broader with the rewrite rule. A lot broader, in fact.

The Proper Way to Do It for Multi-Sites

There’s no need to specify the domain name. Just do this:

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

These rewrite directives say, intercept any request that starts either http://www. or www. and strip out the www; while you’re at it, remember the rest of the request and add http:// to the front of it.

So, whether the hostname contains example.com, bonus.example.com, special.bonus.example.com or anything else, whenever it begins http://www or www it will be converted to plain old http://. This works with WordPress multisite and any other site that uses virtual (aka dynamic) subdomains.

WordPress should automatically rewrite URLs as either http:// or http://www. It depends how you have the URL written in Settings > General > WordPress Address and Site Address. Leave out the www if you  don’t want it to show.

For some older sites, forcing URLs to drop www can cause images and videos to not display.

Is it a Good Idea to Strip WWW?

I believe so.

http:// and http://www can be considered to be two different sites. Consistent use of either http:// or http://www prevents search engines seeing your pages twice.

When someone backlinks to a page using the URL http://example.com and someone else backlinks to a page as http://www.example.com, a search engine following those backlinks will index the same page as though it is two separate pages. Search engines are programmed to penalize duplicate content. One of those pages will be labeled spam and the webmaster might appear to be using blackhat SEO techniques.

Consider that 1,000 people might backlink to the http://www version of a page and another 2,000 people backlink to the www version of the same page; also consider that search engines value backlinks. Isn’t it better to have 3,000 backlinks to one page than to have 3,000 split between two pages that display the same content?


All original content on these pages is fingerprinted and certified by Digiprove