Naked domains and custom domains on CloudFlare and PHPFog

Alastair Aitken 4 July 2011 0




For our new web site design we decided to go with a cloud-based host, settling upon recent startup PHP Fog which allows a very quick setup of WordPress.

Whilst under development the site was located at a PHP Fog domain, http://morganhill.phpfogapp.com, but to make the site live required pointing our DNS at PHP Fog’s service.

For reliability, PHP Fog states that to host a site on a custom domain requires the use of CNAME records for the root domain rather than A records:

yourdomain.com should redirect to www.yourdomain.com to avoid using A records for reliability.
www.yourdomain.com should have a CNAME record to cname01.phpfog.com
Alternatively, if your DNS provider supports this, you can add a CNAME from the root (yourdomain.com) to cname01.phpfog.com directly.

However, a root domain (also know as the zone apex or naked domain) is supposed to be an A record, and A records must point to an IP address.

Whilst allowing PHP Fog to handle problems in a dynamic manner, this presented a problem with our domain registrar (Fasthosts), as their name servers did not appear to allow redirecting the root domain (maybe they can and it’s not obvious). This problem will also be encountered when using cloud services such as Amazon Web Services (AWS).

CloudFlare is very flexible in what can and can’t be done with records, even allowing CNAME records as the zone apex, so we created an account and used their name servers instead.

Setting up an account with CloudFlare

When creating an account, CloudFlare interrogates existing name servers for as many A and MX records as it can. There are some records, particularly CNAMEs, that it can’t detect so this is left to the administrator to enter manually. Once the account is set up, the name servers are changed at the domain registrar to point to CloudFlare.

Switching to a custom domain at PHP Fog

Switching from a PHP Fog domain to a custom domain was straightforward. From PHP Fog’s administration screen:
PHP Fog > Clouds > Shared Cloud > Select app > Settings > Domain Name > Enter custom domain (e.g. morganhill.co.uk) > ‘Update’

Switching to a naked domain at CloudFlare

The records at CloudFlare were updated to the following:

CNAME morganhill.co.uk is an alias of www.morganhill.co.uk
CNAME www is an alias of cname01.phpfog.com

Therefore morganhill.co.uk is redirected to www.morganhill.co.uk, which becomes the canonical URL, which keeps our SEO happy.

Change domain names in WordPress

WordPress embeds references to the domain name in its data, therefore records in WordPress have to be updated to reflect the new domain; it’s easiest to do this via SQL.

The process can be started via the PHP Fog admin site:
PHP Fog > Clouds > Shared Cloud > morganhill.co.uk > Database > Access the Database through phpMyAdmin

This launches phpMyAdmin where the following three statements were executed:

UPDATE wp_options SET option_value = replace(option_value, 'http://morganhill.phpfogapp.com','http://www.morganhill.co.uk ') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://morganhill.phpfogapp.com','http://www.morganhill.co.uk');

UPDATE wp_posts SET post_content = replace(post_content, 'http://morganhill.phpfogapp.com', 'http://www.morganhill.co.uk');

Caveat

Backup everything first!




Alastair Aitken (124 Posts)

As a contract developer and manager I’ve worked in a wide range of enterprises in a variety of countries where I’ve encountered everything from great work, awful work, bizarre work, all the way down to quasi-legal work. If you think that you recognise your own organisation within my articles then you’re undoubtedly wrong, where you work isn’t that unique.

Leave A Response »