9

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

Abhishek
  • 3,900
  • 21
  • 42

5 Answers5

7

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.

dhinchliff
  • 1,106
  • 8
  • 17
2

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

StR
  • 545
  • 6
  • 17
1
RewriteEngine on
RewriteCond %{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.

Diogo Jesus
  • 318
  • 4
  • 19
1

Once you have the redirec from the clients domains to the domain Try this in apache site configuration example.com.conf(all in the same file.) or you can do it through IP Based (Apache Source)

Listen 80

<VirtualHost *:80>
    DocumentRoot "/www/example/client1/"
    ServerName client1.com
    ServerAlias www.client1.com
    #Server Options
    #Directory Options
    #Log Options
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/www/example/client2/"
    ServerName client2.com
    ServerAlias www.client2.com
    #Server Options
    #Directory Options
    #Log Options
</VirtualHost>
Ellesar
  • 341
  • 1
  • 11
  • That will need 2 separated copies of the same system. I understand the goal is to use the same pages or same code for different domains. – StR Mar 14 '18 at 23:29
  • Not really that should work with his configuration. `from client1.com to example.com/client1` `from client2.com to example.com/client2` Apache should redirect to the right folder acording to the ServerName/Alias provided by the client name. If that dosen't work then he i'll need to use re direct by IPs as stated in the Apache Source. – Ellesar Mar 15 '18 at 04:13
1

As you have a single Laravel Application with multiple domains I suggest you to follow below steps to solve your problem.

Domain configurations in Apache:

ServerName dev.laravel.com
ServerAlias dev.laravel-client1.com dev.laravel-client2.com

Laravel Application Route Configurations:

for: dev.laravel-client1.com

Route::domain('dev.laravel-client1.com')->group(function () {
    /* client2 domain routes here */
    Route::get('/', function () {
        return view('client1');
    });

    Route::get('/something', function () {
        return "Something at client 1";
    });
});

for: dev.laravel-client2.com

Route::domain('dev.laravel-client2.com')->group(function () {
    /* client2 domain routes here */
    Route::get('/', function () {
        return view('client2');
    });
});

Note: This is not a recommended method for unlimited domains

Praneeth Nidarshan
  • 1,704
  • 15
  • 21