Support the ongoing development of →
posted 10 years ago
Last updated 2 years ago.

Yes, this is possible. I did something similar, until I realized that subdomains really ain't that great (for me) and a first-level route is better. Anyways, in your apache configuration you'd do something like this for every "main" subdomain:

<VirtualHost *:80>
                DocumentRoot /var/www/staging/public
                ServerAlias *
                SetEnv MY_LARAVEL_ENV "staging"
                <Directory /var/www/staging/public>
                        <IfModule mod_rewrite.c>
                                Options -MultiViews
                                RewriteEngine On
                                RewriteCond %{REQUEST_FILENAME} !-f
                                RewriteRule ^ index.php [L]

Then at the top of your route file, you can grab the environment you set with apache above. Something like this for different main domains:

if (App::environment() == 'staging')
	$domain = '';
elseif (App::environment() == 'live')
	$domain = '';

Route::group(array('domain' => '{company}.'.$domain), function()
    /* put your routes here */
    Route::get('/', 'YourController@getCompany');
Last updated 2 years ago.

Thanks. I also have a local environment set up, with the form Unfortunately, I have to do php artisan serve to run the app properly and then I access it with localhost:8000. When I try to route like test.localhost:8000, it fails. Any ideas for getting around this? How did you handle local development? Thanks!

Last updated 2 years ago.

did you found any solutions?


For local development, you need a set of hostnames that point to the local host. You can set them up for just yourself, with edits to /etc/hosts on a unix/mac system, or to C:\Windows\System32\Drivers\etc\hosts on Windows. You can just make a list: localhost thing.localhost thing2.localhost, or even use any arbitrary names (even ones you don't control, this is just changing your computer's view)

You can also do this in DNS — on any domain name you can add records like so:

thing1.local IN A
thing2.local IN A
thing3.local IN A

And then access if you did this for the domain.

It's best to use something that's at least three parts (separated by .) because the rules around cookies and subdomains are weird and counterintuitive and trying to do multiple domains under localhost without a third part makes some of those weird rules bite you.

Last updated 6 years ago.

Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.