Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How to open multiple domains (not subdomains) to show pages (no redirection) from same server?

I have a Laravel project hosted with a domain say: example.com. I have several clients domain say client1.com, client2.com etc. I need to have a system (say apache configuration) in a way that if someone types client1.com it should show a page from example.com/client1.com

like image 444
Abhishek Avatar asked Feb 24 '18 19:02

Abhishek


2 Answers

What you are doing seems quite hacky and you may want to try another approach, however if you insist on this approach and don't want to issue redirects you may want to try using Apache as a proxy. Try a vhost like this:

<VirtualHost *:80>
    ServerName client1.com
    ProxyPassMatch "^/(.*)$" "http://example.com/client1.com/$1"
</VirtualHost>

I have not tested it but it should give you an idea.

This mechanism will not re-write the body of the response, so you may have multiple problems, for example with urls in links. Make sure the internal client apps use relative urls.

like image 131
dhinchliff Avatar answered Oct 15 '22 00:10

dhinchliff


What you want to do needs 2 steps. The first one is to tell Apache to point the domains to the same Laravel Application.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias client1.com, client2.com, client3.com
    DocumentRoot /path/to/your/laravel/public/
    <Directory "/path/to/your/laravel/public/">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Once all your client domains point to the same Laravel app, you can just read the servername in your controller and pass the servername to your view. Something like this:

<?php
class YourController extends Controller
{
    public function index(Client $client)
    {
        $domain =  $_SERVER['SERVER_name'];
        return view('my.view', ['client' => $domain]);
    }
}

After you visit client1.com/foo/bar the $domain variable will have client1.com

like image 44
StR Avatar answered Oct 15 '22 00:10

StR