3

I'm creating web application which needs dynamic domain per user.

Let say I've 5 users in my DB such as.

1) wanda
2) edward
3) rick
4) maurice
5) kristin

Now I've a domain as example.com.

I want to get data of that 5 users as per his domain.

1) wanda.example.com
2) edward.example.com
3) rick.example.com
4) maurice.example.com
5) kristin.example.com

route.php

Route::group(['domain' => '{user}.example.com'], function () {
   Route::get('/', function ($user) {
        echo $user;
        exit;
    });
});

I'm using Xampp, Apache server so I configure httpd-vhosts file as

C:\xampp\apache\conf\extra\httpd-vhosts

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/myproject"
    ServerName example.com
    ServerAlias *.example.com
    DirectoryIndex index.php
    <directory "C:/xampp/htdocs/myproject">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from all
    </directory>
</VirtualHost>

C:\Windows\System32\drivers\etc\hosts file

127.0.0.1   example.com  
127.0.0.1 *.example.com

When I access user.example.com I'm Getting this error.

enter image description here

I want this type of dynamic domain as works.

http://wanda.thewallchat.com/
http://edward.thewallchat.com/
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
  • The Hosts file does not support a subdomain wildcard, you will have to list every subdomain individually. See [here](https://superuser.com/a/135599) and [here](https://stackoverflow.com/a/20446931/9193055) – Remul Sep 19 '19 at 08:52
  • In the future there will be many users is going to register in my web app so as per this https://superuser.com/questions/135595/using-wildcards-in-names-in-windows-hosts-file/135599#135599 I can't list every subdomain after register. Is there not any way to do using .htaccess or any other way to do this? – Dilip Hirapara Sep 19 '19 at 08:55
  • @Laravel it depends on where you host your application. As in aws you can you route 53 for your requirement. – LulzAsh Sep 19 '19 at 08:58
  • Right now I'm trying it in local server. is this works for me on live server? `you can you route 53 for your requirement` can you elaborate it? – Dilip Hirapara Sep 19 '19 at 08:59
  • 1
    @Laravel yes for testing in local make separate aliases and when you will deploy you need to configure wildcard subdomain on live server. – LulzAsh Sep 19 '19 at 09:02
  • configured wildcard subdomain It's working on live server. – Dilip Hirapara Sep 19 '19 at 09:34

1 Answers1

2

You cannot write * for all in <VirtualHost *:80> you need to specify each alias which you want to use in your system.

change

ServerAlias *.example.com

to

ServerAlias example.com wanda.example.com edward.example.com rick.example.com maurice.example.com kristin.example.com

Don't forget to restart apache.

If you want to setup on live server configure wildcard subdomain.

ServerAlias *.example.com
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
LulzAsh
  • 611
  • 1
  • 7
  • 23
  • Thank you @LulzAsh for answer. But in the future, there will be many users in db, So every time should I defined it as ServerAlias ?? it there not any other way to do it dynamic? – Dilip Hirapara Sep 19 '19 at 08:57