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
-
Downvoters could you please reply here why this needs to be downvoted? – Abhishek Feb 24 '18 at 19:37
-
If lack of research is the reason I got this: https://stackoverflow.com/questions/16353159/how-to-point-two-different-domain-names-to-a-single-website but this doesn't answer my question and hence I asked here. – Abhishek Feb 24 '18 at 19:38
-
Only show or redirect to example.com/client1.com? – iwex Mar 15 '18 at 16:36
-
1What have you tried? What kind of page are you trying to show? How is this specific to Laravel? Do you want to do a redirect or transparently rewrite the URL? – Erik Berkun-Drevnig Mar 15 '18 at 20:10
-
1Can't you just point the domain to the same DNS record and use http_host to check what domain is currently opened? – Ron van der Heijden Mar 16 '18 at 08:21
-
To clarify: Do you manage everything through a single Laravel Application or are there separate applications for each clients? – Praneeth Nidarshan Mar 17 '18 at 02:11
-
1I am using single Laravel application @PraneethNidarshan – Abhishek Mar 17 '18 at 06:16
-
will your client domain count increase in future? and have you tried Laravel Route method for domains? – Praneeth Nidarshan Mar 17 '18 at 10:10
-
@Abhishek have you tried my solution? – Praneeth Nidarshan Mar 18 '18 at 15:04
5 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.

- 1,106
- 8
- 17
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

- 545
- 6
- 17
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.

- 318
- 4
- 19
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>

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

- 1,704
- 15
- 21