Search code examples
phplaravelurl-routing

How to use multiple domains with different pages and routes in Laravel?


How to use multiple domains with different pages and routes in Laravel?


Solution

  • I spent many hours looking for solutions, but nothing concrete, always with complex and messy codes, in the end I developed a practical solution with clean code.

    Shared Hosting

    1 - Firstly, it is necessary to centralize the laravel in a single domain, then you must point the other domains to the main domain, you can access your dns manager and use the CNAME record for this.

    Note: This step is only necessary if you are using shared hosting, if this is not your case, instead of changing the DNS zone, prefer to change the root folder of the domains involved to the public folder of your laravel project. You can do this by changing the virtual host that manages your domain on Apache, Nginx or another web server you are using.

    2 - In your Laravel you must create a Controller the home page with the following content, replacing what is necessary:

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $origin = array("mydomain.com", "mydomain2.com");
        $domain = parse_url(request()->root())['host'];
    
        if (in_array($domain, $origin)) {
            if ($domain === 'mydomain.com') {
                return view('myview'));
            } 
            if ($domain === 'mydomain2.com') {
                return view('myview2'));
            } 
        } else{ 
            return view('unauthorized');
        } 
    }
    

    3 - Finally (optional), create a route with the urls that will be accessible only by that domain, do so:

    routes/web.php or routes/api.php (if you are creating API. Also works)

    Route::group(array('domain' => 'mydomain.com'), function () {
        /* routes here */
        Route::get('/', 'YouController@index');
    });
    
    Route::group(array('domain' => 'mydomain2.com'), function () {
        /* routes here */
        Route::get('/', 'YouController@index');
    });
    

    You must change mydomain.com and mydomain2.com to the domain you want, else{} you must replace unauthorized with a valid view, this is what will appear when the domain is not listed, if you want you can do the o server also shows nothing.

    VPS hosting or your own server

    1 - Firstly, you need to change the virtual host of each domain that you will use in laravel and point the root folder to laravel's public folder, this way, laravel's routing will work correctly and your website will be displayed according to the domain.

    Assuming you are using Apache and this is your current virtual host:

    # Ensure that Apache listens on port 80
    Listen 80
    <VirtualHost *:80>
        DocumentRoot "/www/public_html"
        ServerName example.com
        ServerAlias www.example.com
    
        # Other directives here
    </VirtualHost>
    

    You will change the DocumentRoot to the laravel public folder path, let's say the folder is "mylaravel", it would look like this:

    # Ensure that Apache listens on port 80
    Listen 80
    <VirtualHost *:80>
        DocumentRoot "/www/public_html/mylaravel/public"
        ServerName example.com
        ServerAlias www.example.com
    
        # Other directives here
    </VirtualHost>
    

    2 – Now to finish it is necessary to configure the routes. All you need to do is place your routes within the Route::group of each domain as in the model below:

    routes/web.php or routes/api.php (if you are creating API. Also works)

    Route::group(array('domain' => 'mydomain.com'), function () {
        /* routes here */
        Route::get('/', 'YouController@index');
    });
    
    Route::group(array('domain' => 'mydomain2.com'), function () {
        /* routes here */
        Route::get('/', 'YouController@index');
    });
    

    By doing this, you can use Laravel normally, there is nothing else you need to do, everything is ready, regardless of whether you are using blade or jetstream.

    Remember that to see these routes on localhost, you must remove the Route::group from the domain, as these routes only work when the user accesses from the linked domain.