22 March 2021

When you migrate a website to a new server you need to test the website on the new server before updating the DNS. This article looks at a few different way to do this.

Bypassing DNS resolution using your hosts file

The easiest way to check a website as it appears on a new server is by updating your hosts file. For instance, for this knowledgebase we often use the domain example.com. This domain is a so-called special-use domain that may be used for documentation and testing. We don’t own the domain, but we have an account for the domain on our Strawberry server. The server’s IP address is 84.18.206.207, so to view the website as it appears on the server you can add this to your hosts file:

84.18.206.207 example.com www.example.com

The line tells your computer’s resolver that the domains example.com and www.example.com resolve to the IP address 84.18.206.207. Because the domain’s A record is defined locally the resolver won’t do a DNS lookup.

On Unix systems (macOS, Linux etc.) you can edit the /etc/hosts file with root privileges. On Windows you need to edit C:\Windows\System32\Drivers\etc\hosts and reboot your PC (the reboot isn’t needed on Unix-like operating systems). If you regularly edit your hosts file on Windows then you can install the open source Hosts File Editor Scott Lerch.

Ping

You can verify to which IP address a domain resolves with ping. On OSX and Linux you can run ping -c 3 example.com from a terminal window. On Windows you can run ping example.com from the command prompt.

$ ping -c 3 example.com
PING example.com (84.18.206.207) 56(84) bytes of data.
64 bytes from example.com (84.18.206.207): icmp_seq=1 ttl=51 time=16.8 ms
64 bytes from example.com (84.18.206.207): icmp_seq=2 ttl=51 time=16.5 ms
64 bytes from example.com (84.18.206.207): icmp_seq=3 ttl=51 time=29.3 ms

--- example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 16.489/20.859/29.262/5.943 ms

Per-user web directories

Depending on whether or not Apache’s userdir module is enabled on your server you can also use per-user web directories. For instance, the example.com website is owned by the user example and the server’s hostname is strawberry.active-ns.com. I can therefore view the website via https://strawberry.active-ns.com/~example.

It’s worth noting that your browser will show an SSL warning when you use per user web directories. Also, per-user directories aren’t enabled on all servers. For instance, the userdir module is disabled when the mod_ruid2 or npm_itk modules are installed.

Using an alias

Another option is to use an alias (also known as a parked domain). For instance, let’s say you got the (non-existing) domain alias.url. You can change the domain’s A record to the IP address of the new example.com server and add alias.url as an alias on the example.com account. The domain alias.url now shows the content of the example.com website on the new server.

You don’t need to use a spare domain. Instead, you can use one of our aliases. On most of our servers we use the domain temporarywebsiteaddress.com for aliases. For instance, for the Strawberry server we got an A record for *.strawberry.temporarywebsiteaddress.com:

$ dig *.strawberry.temporarywebsiteaddress.com A +short
84.18.206.207

You can use this to add an alias. You can simply replace the asterix (*) with any alphanumeric string. For instance, you can create the alias example.strawberry.temporarywebsiteaddress.com to view your new example.com website. This alias has two advantages over per-user web directories:

  • They always work, regardless of the server configuration.
  • You can get a Let’s Encrypt SSL certificate for the alias (so you don’t get SSL warnings in your browser).

You do need to have a spare alias available. If you don’t, we’re happy to add an alias for you. Another thing to be aware is that Let’s Encrypt refuses to install certificates for domains longer than 63 characters. The domain-part of the alias has 40 characters, so the user-first part should be less than 24 characters.

Base URLs

When you use an alias you need to make sure that your website’s base URL is correct. For instance, if you have a WordPress website then you need to make sure that the website URL (under Settings » General) is set to the alias. The base URL is used to display resources such as images and for internal links. If you don’t change the URL your website will look broken.

And, of course, when the website is ready to go live you need to change the base URL back to what it was. You need to do this at the same time you update the DNS for your domain.