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

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


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.


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:

<?phpclass 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


RewriteEngine onRewriteCond %{HTTP_HOST} ^client1\.com$ [NC]RewriteRule ^(.*)$ http://www.example.com/client1.com/ [R=301,L]

This should do the trick. You can add this directly to your client apache config or create a .htaccess file on each client webroot. Both ways should do the trick.