0

I know that you can except some URIs of your main app like if you want to except example.com/page, you can just simply add it to the CheckForMaintenanceMode.php, like this:

In app/Http/Middleware/CheckForMaintenanceMode.php

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;

class CheckForMaintenanceMode extends Middleware
{
    /**
     * The URIs that should be reachable while maintenance mode is enabled.
     *
     * @var array
     */
    protected $except = [
        '/page'
    ];
}

Now, my app has a couple subdomains using one app; I have a subdomain for my main app: app.example.com, a subdomain for my API endpoints: api.example.com and the main website: www.example.com

How can I possibly except the specific subdomain instead of URI for maintenance mode? Like having api.example.com and app.example.com in maintenance mode but not the main website www.example.com?

I'm trying to figure out it on my own and even make my own middleware just to do this, but is it possible to do this using the built-in maintenance mode of laravel with php artisan:down?

Something like:

// app.example.com and api.example.com is in maintenance mode except:

protected $except = [
    'example.com'
    'www.example.com'
];
4
  • cant you just add 'www.example.com/*' to the except array?
    – Flame
    May 14, 2020 at 11:33
  • @Flame I did that, it doesn't work. I guess that the $except object is only meant for URIs May 14, 2020 at 11:35
  • 1
    Investigate the CheckForMaintenanceMode class, it calls a $request->fullUrlIs($except) function. I havent tested its inner workings but I think it should work with the full url. You obviously have to test this in such way that youre not using http://localhost in your browser.
    – Flame
    May 14, 2020 at 11:43
  • @Flame It actually works with protocol. Please add it to the answer so I can accept. Thank you. May 14, 2020 at 11:48

2 Answers 2

0

See the Illuminate\Foundation\Http\Middleware\CheckMaintenanceMode middleware class:

It checks the elements of the $except property using the function fullUrlIs() from the Illuminate\Http\Request class, which itself calls the Str::is() helper (also known as the str_is() function if you're using Laravel helper function globals):

protected function inExceptArray($request)
    {
        foreach ($this->except as $except) {
            if ($except !== '/') {
                $except = trim($except, '/');
            }

            if ($request->fullUrlIs($except) || $request->is($except)) {
                return true;
            }
        }

        return false;
    }

See https://laravel.com/docs/7.x/helpers#method-str-is

You should then be able to check for an url like this, to exclude this domain from the maintenance mode (i.e. it will always be up):

protected $except = [
    'https://www.example.com/*'
];
1
  • You also have to include both https://www.example.com/ and https://www.example.com/* so the main URL and its paths become accessible. May 14, 2020 at 12:49
0

Suppose you have two domains. One is the main domain and another one is a subdomain.

  • mydomain.com
  • admin.mydomain.com

You have a page name maintenance. The maintenance page is under the main domain. The URL of the maintenance page is mydomain.com/maintenance. In the maintenance mode, you will have the route permission of mydomain.com/maintenance and admin.mydomain.com

Now work process. Goto App\Http\Middleware the open the PreventRequestsDuringMaintenance middleware then add this code

protected $except = [
   'maintenance*',
   'http://admin.*',
   'https://admin.*'
];

Then go to App\Exceptions open Handler file, inside render function add

if (App::isDownForMaintenance()) {
   return redirect('/maintenance');
}

Now run php artisan down

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.