Reputation: 3967
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
Upvotes: 10
Views: 3332
Reputation: 1704
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
Upvotes: 1
Reputation: 561
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
Upvotes: 2
Reputation: 351
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>
Upvotes: 1
Reputation: 318
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.
Upvotes: 1
Reputation: 1194
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.
Upvotes: 7