Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhand's avatar
Level 6

Post php artisan config:cache routes fail to load properly

When I run

php artisan config:clear   
Configuration cache cleared!
php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!

I can no longer load routes to published sites. I mean, it tries to load the route to the application's domain instead, not the published site like sub.domain.test. It just tries to load domain.test (main app domain) . However when I then run:

php artisan optimize       
Configuration cache cleared!
Configuration cached successfully!
Route cache cleared!
Routes cached successfully!
Files cached successfully!

all is well again. Any ideas why this could be the case? Does the cache clearing remove the routes' cache too? But why would it not be cached on running php artisan config:cache then?

0 likes
10 replies
sr57's avatar

@rhand

it tries to load

Don't understand

it ?

What command did you enter ? Should be related to your browser cache ...

What's your APP_URL vs your web server & D?S config?

1 like
rhand's avatar
Level 6

@sr57 It tried to load en.domain.test instead of some-other-subdomain.domain.test and fails as content cannot be loaded properly. So it used the wrong route to app url instead of published site.

Last command before site loading failure with error as wrong url was being loaded was php artisan config:cache. I did do php artisan cache:clear once before the config clear command which is application cache .. Perhaps that was the issue.. But does php artisan optimize then create config, application and routes cache? And still not sure why it would cause issues..

History shows I ran these

10724  php artisan cache:clear
10725  php artisan config:optimize // does not work
10726  php artisan config:clear
10727  php artisan config:cache
10728  php artisan optimize

en app url:

APP_URL=https://domain.test
APP_DOMAIN=domain.test

and Nginx set up with same domain as app url using Laravel Valet. Well and made it wildcard to deal with subdomains as well.

sr57's avatar

@rhand

Sorry but I still don't understand well

First : there is no command "php artisan config:optimize"

Can you only describe your pb by :

command :

result :

expected result :

in the 2 cases : after config:clear & config:cache

rhand's avatar
Level 6

@sr57 Yes, php artisan config:optimize does not exist. Added that as comment to command history list. (updated reply).

I ran php artisan cache clear, php artisan config:cache and expected the published sites to load locally. But instead routes failed to work and all published sites directed to application url instead.

At https://stackoverflow.com/questions/37878951/how-to-clear-laravel-route-caching-on-server I read that php artisan cache:clear clears cached routes. So perhaps that initial command removed the application cache including routes? And the on php artisan optimize I rebuilt all so also routes' cache?

sr57's avatar

perhaps ?

php artisan list
1 like
Snapey's avatar
Snapey
Best Answer
Level 122

You are relying on some value from the .env file somewhere (probably) in your wildcard routing.

never use .env values outside of configuration files.

1 like
rhand's avatar
Level 6

@Snapey We do have

...
Route::group(['domain' => env('APP_DOMAIN')], function () {
    Route::get('/', 'HomesController@index');
...

in web.php Could that be it that needs me to cache routes using php artisan optimize and otherwise get issues with published sites not registering properly and being redirected to app url?

Or site.php config file with:

<?php

return [
    'excluded_domains' => env('EXCLUDED_DOMAINS'),
    'google_maps_api' => env('GOOGLE_MAPS_API'),
];

or app.php loading

'name' => env('APP_NAME', 'Laravel'),

    'domain' => env('APP_DOMAIN', 'site.com'),
    'client_project_domain' => env('CLIENT_PROJECT_DOMAIN', 'site.com'),
    'admin_project_domain' => env('ADMIN_PROJECT_DOMAIN', 'site.com'),
Sinnbeck's avatar

@rhand Like @snapey said. Never use env outside of config files. As soon as you turn on cache ALL env() calls return null (so this will break env('APP_DOMAIN'))

So move that to a config file, and load the value from there.

1 like
rhand's avatar
Level 6

@Sinnbeck I see, yeah, okay. Do recall we moved some more to config files in the past. Just need to see how I can load the APP_DOMAIN in the web.php file. Think something like

'domain' => config('smt.app_domain'),

and

'app_domain' => env('APP_DOMAIN'),

in smt.php config

Please or to participate in this conversation.