We often have clients that set-up a staging environment in addition to their production e-commerce environment. We also do a lot of website moves for clients who want to see their site on our servers before the final cut-over takes place. While many web applications are domain agnostic Magento is finicky about being run under the domain its configured with. To complicate things the domain may be used by add-ons or extensions that require the “live” domain to be used.
The simple trick we often use to make the development and/or newly moved site look like they are running under the final domain is to use the hosts file. To understand the hosts file you need to understand some basics about how the Internet works. The Internet uses IP addresses to denote a location, like your web store or your home computer. Instead of having to converse using IP addresses, which can be hard to remember (and with IPv6 impossible to remember) the Internet uses the Domain Name System (DNS). DNS has the simple job of translating names (like www.nexcess.net) to IP addresses. When you type www.nexcess.net into your browser it uses your operating system’s networking subsystem to do the DNS translation from name to IP.
Once nice feature of operating systems (Linux, Mac OS, etc) is that they allow you to short-circuit this DNS process and provide your own values for any name. For instance, you could change the IP of google.com locally to point to yahoo.com. This change would be local to your machine only and not visible to anyone else. This comes in handy when you know the IP address of your new Magento store (or any other site) and want to see that site under your domain without making this change global. It’s as simple as this:
On Linux (or Mac OS) just edit the file (as the root user) named /etc/hosts (If you’re on Windows the hosts file lives in C:\windows\system32\drivers\etc\hosts). The file has a simple format of 2 columns. The first column is the IP address that you’d like the name to resolve to and the 2nd column is the name itself. So, if I wanted to point www.nexcess.net to go to the IP 220.127.116.11 I’d just edit the /etc/hosts file and add the following line at the bottom:
Now when I go to www.nexcess.net in my browser (or using any other networking function on the system) it will think that 18.104.22.168 is the actual IP address. Don’t remove any entries from the file unless you added them. Removing standard lines from the file can have unintended side-effects.
Now you can test your site as if it were live and make sure any features of the site that require the “real” domain work correctly. REMEMBER to change the hosts file back so you don’t inadvertently make changes to the test area thinking it’s the live area. You’ll be pulling your hair out wondering why your friends on other computers don’t see your edits.