Source: ahrefs.com

We recently set up Google Pagespeed Service for one of our WordPress sites. The site was setup with a “bare” domain name. That is, it had no subdomain. For example:

http://healthfitnessexperts.com

Google’s Pagespeed Service can’t handle bare domains. Like many content delivery networks (CDNs) and similar services, Pagespeed requires a subdomain like www. For example:

http://www.healthfitnessexperts.com

We switched the domain name using the handy WordPress Domain Mapping Plugn. But we needed to 301 permanent redirect all traffic from the bare domain to the www domain.

To do the 301 redirect, we just added this at the beginning of our .htaccess file:

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

Done. That was easy.

(Of course, this all assumes that your DNS is setup correctly, and that you are using Apache with mod-rewrite installed. Otherwise, you have a few more hoops you need to jump through.)