1

I want to have routes like this:

john-doe.mysite.local
john-another-doe.mysite.local

and then pass the subdomain name to some controller@method and present the users profiles.

I'm using Laravel Homestead.

Here's my /etc/hosts file from my local machine:

....
192.168.10.10 mysite.local
192.168.10.10 *.mysite.local
....

and on my virtual machine the /etc/nginx/sites-available/mysite.local file

server {
    listen 80;
    server_name mysite.local *.mysite.local;
    root "/home/vagrant/laravel-projects/mysite.com/public";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    ....

If you need anything else please ask. The main domain mysite.local works normally.

I tried following the documentation an adding this to my routes.php file:

Route::group(['domain' => '{element}.mysite.local'], function() {

    Route::get('/', function ($element) {
        dd($element);
    });

});

But if I go to any subdomain somedomain.mysite.local I get server not found.

It seems to me the problem is that I'm not even reaching Laravel app through the subdomain.

Any help is appreciated. I've found this question, and tried changing .local to .dev in my /etc/hosts and mysite.local files but it didn't work. I also tried changing mysite.local to mysite.dev but it didn't work either.

I can't figure out what am I missing here.

UPDATE:

If I add test.mysite.local to my /etc/hosts file I get the same output in my browser as if I was to browse for mysite.local.

....
192.168.10.10 mysite.local
192.168.10.10 test.mysite.local
192.168.10.10 *.mysite.local
....

Updating the mysite.local on virtual machine:

...
server_name mysite.local test.mysite.local *.mysite.local;
...

I tried adding another route to routes.php:

Route::group(['domain' => 'test.mysite.local'], function() {

    Route::get('/', function () {
        dd('you are at test subdomain');
    });

});

to see if it works but it doesn't, I still get the same output as if I were to navigate to mysite.local

ANOTHER UPDATE:

/etc/hosts/ doesn't support wildcards so for the moment I'm using static subdomain names and will figure out how to do dynamic subdomains a bit later.

Also put

Route::group(['domain' => '{element}.mysite.local'], function() {

    Route::get('/', function ($element) {
        dd($element);
    });

});

AT THE TOP or your routes.php file.

At the moment I'm trying to redirect static subdomain to the specific controller@method. Will update this as I progress.

UPDATE - static domain working now:

New route:

Route::group(['domain' => '{element}.mysite.local'], function() {

    Route::get('{path}', 'ElementController@single')->where('path', '.*');

});

ElementController.php

public function single($element)
{
    //return view with information about my element
    if ( $element = Element::where('slug', $element)->first() )
    {
        return view('elements.single', compact('element'));
    }

    //redirect to mysite.local if slug doesn't exist
    return redirect(Config::get('app.url')); // points to config/app.php and find `url` parameter which should be defined in your `.env` file

}

TODO:

  • slug.mysite.local/sadsadas works and runs the single method from above, I would prefer if it would redirect to slug.mysite.local
  • wildcard subdomains still not working
Community
  • 1
  • 1
dbr
  • 1,037
  • 14
  • 34

1 Answers1

0

Maybe this? 168.168.10.10 in your hosts.... it seems have to be 192.168.10.10

Hans Yulian
  • 1,080
  • 8
  • 26
  • Nothing, but now I need to try again with changing the `.local` to `.dev` because I'm not sure when did this typo occur. Thanks! – dbr Jul 08 '16 at 02:55
  • I tried changing the `.local` to `.dev` in `hosts` and `mysite.local` files. Also I copied `mysite.local` to `mysite.dev` but it didn't work. However, this brought another problem and that is when I opened `mysite.dev` in my browser I got presented with another site I have locally that is also defined in my `hosts` file. It redirected to it even if I commented it in the `hosts` file. It seems I'm missing something big here. – dbr Jul 08 '16 at 03:04
  • can you confirm that the nginx configuration already working well? i mean try to use simple domain without subdomain first and then simple routes in laravel.. see if the page is opened or not – Hans Yulian Jul 08 '16 at 03:19
  • Yes, the TLD is working well. It's just the subdomains I can't reach. – dbr Jul 08 '16 at 12:11
  • I updated the `/etc/hosts` with `192.168.10.10 test.mysite.local` and when I point to it with my browser I get same response as if I was to go to `mysite.local`. – dbr Jul 08 '16 at 14:18