Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

macmotp's avatar

Homestead and Subdomains

Hello everyone,

I am trying to use subdomains locally with Homestead and VM, but I really cannot find the correct way to implement it.

My target is to use, for example, subdomain.site.app and site.app separately accordantly with my routes.

I tried to setup the file Homestead.yaml with two separates maps but it is not working. I am wondering if anyone had tried this and if it is a nice idea for the purpose of using separate subdomains in a production environment (otherwise please let me know any other options).

Thank you,

Marco

0 likes
21 replies
bashy's avatar

Depends at what level you want to manage the domains but I think you want different routes depending on if it's subdomain or not?

Laravel routes are also able to handle wildcard sub-domains, and will pass your wildcard parameters from the domain:

Registering Sub-Domain Routes

Route::group(array('domain' => '{account}.myapp.com'), function()
{

    Route::get('user/{id}', function($account, $id)
    {
        //
    });

});

Ref: http://laravel.com/docs/4.2/routing#sub-domain-routing

macmotp's avatar

Thank you @faisal_arbain and @bashy for your reply. When I submit the question I forgot to mention that I had already added the subdomain in the hosts file and used the sub-domain routing. My problem was related to an incorrect configuration of Homestead. Here is the solution:

1) add all the subdomains you need on /etc/hosts:

127.0.0.1 site.app
127.0.0.1 subdomain.site.app

2) map all the subdomains in the file Homestead.yaml pointing to the same root (this was the issue because I needed to run "vagrant provision" and not just "vagrant up"):

sites:
    - map: site.app
      to: /home/vagrant/site/public
    - map: subdomain.site.app
      to: /home/vagrant/site/public

3) use sub-domain routing in Laravel as follow:

$router->group(array('domain' => 'site.app'), function()
{
    get('/', function() {
        return 'main app!';
    });
});

$router->group(array('domain' => 'subdomain.site.app'), function()
{
    get('/', function() {
        return 'subdomain app!';
    });
});

12 likes
bashy's avatar

Oh right, always best to explain what you've done already :)

Glad you got it sorted!

joshuahornby's avatar

Out of interest does anyone know how to get this working with wildcard subdomains for Homestead?

bashy's avatar

What do you want to allow? Pick up all subdomains in Nginx?

ross.edman's avatar

@joshuahornby Do you mind elaborating on how you got this to work? I have tried quite a few things and cannot get this to work.

I have confirmed DNSmasq is working. I installed through homebrew. I also altered the serve.sh file in Homebrew to include a wildcard in front of the server_name and I still cannot get anything to work.

joshuahornby's avatar

@ross.edman Sure, after setting up DNSMasq I homestead ssh and then edited /etc/nginx/sites-available/mydomain" and added the wildcard before the domain in there, so I have something like

server_name  example.org  *.example.org;

Then I just restarted homestead.

4 likes
neoish's avatar

@joshuahornby When I restart Homestead with provisioning it gets overridden :S

EDIT: Nevermind I have figured it out

domandtom's avatar

Just did this. Here's my configuration:

On homestead vagrant box (to access: homestead ssh)

  • sudo vim /etc/nginx/sites-available/homestead.app: server_name homestead.app *.homestead.app;

On Mac:

  • sudo vim /etc/hosts: 192.168.10.10 homestead.app
  • sudo vim /usr/local/etc/dnsmasq.conf: 'address=/app/192.168.10.10'
  • sudo vim /etc/resolver/app: nameserver 127.0.0.1
6 likes
raffe's avatar

Anyone got this working with Ubuntu? All info I find seems to be for OS X and I just can't get it to work.

bbouton's avatar

I simply added the following to my site declarations in sites.yaml

  • map: '*.mysite.dev' to: /mycode/on/vagrant

single quotes worked. I also use dnsmasq for all .dev domains to point to the homestead IP.

1 like
BartHuis's avatar

the solution of @domandtom worked for me, if you have an other subdomain not working on windows with homestead/vagrant: log into your vagrant (vagrant ssh)

cd /etc/nginx/sites-available/
ls

here are the files to edit for your domains, look up your domain, for example devdomain.dev, and then edit that one:

sudo vim devdomain.dev

look for the line where your domain is, in this example devdomain.dev:

server_name devdomain.dev;

and add the * domain to the same row like this:

server_name devdomain.dev *.devdomain.dev;

ait: thought it worked, but still didn't managed to get api. been handled by the passport token verivication the right way. we're changing it back to /api again, but if someone wants other subdomains working in homestad/vagrant on windows, you can use the info above.

Bart

1 like
devd's avatar

@domandtom Dude you just saved my day. Spent almost a trying to configure wildcard subdomain on my homestead. "sudo vim /etc/resolver/app: nameserver 127.0.0.1" was the saviour.

Kudos (Y)

Please or to participate in this conversation.