← Back to all posts

How to set up a subdomain on Rails localhost

David Anderson | Apr 20, 2019

I recently worked on a client project that required setting up a subdomain for each of the client’s customers. For example, they wanted to use company1.everclimb.co for Company 1 and company2.everclimb.co for Company 2. Though the app was essentially the same for both Company 1 and Company 2 there were a few things that changed based upon the subdomain, such as displaying the appropriate company’s logo in the nav bar. I managed to test this locally by editing my hosts file.

You can find your hosts file here: /private/etc/hosts Because the file is locked, I had to find the File using Finder, move it to the desktop, edit it, save it, and then move it back to /private/etc/

Finder --> Go --> Go to folder --> /private/etc/hosts

Move the file to your Desktop and open it using your text editor. You can add something like:

127.0.0.1       abcwidgets.local.host

Save the file and move it back into /private/etc. Navigate to your rails app and run the server rails s. Assuming you’re running on the default port (3000), you should now be able to access the subdomain at

abcwidgets.local.host:3000

Good luck!

SUBSCRIBE

Drop your email in the box below to subscribe to my newsletter. Once per week you'll get Ruby/Rails tips, guides, job postings, and general thoughts from the web developer trenches.