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

martinszeltins's avatar

How to get rid of the hardcoded "localhost" in .env files?

I have 2 .env files (one for Vue and one for Laravel). Both of them have localhost hard coded inside of them. That means that I can't access my application from another computer on my network.

For example if I go to my other PC and access my site at http://192.168.1.100:47344 then it won't work because it is hardcoded to localhost.

But I can't write javascript or PHP inside .env files to change localhost to something like window.location.host and for PHP $_SERVER['SERVER_ADDR']. I can't find the solultion..

My Vue .env

VITE_SERVER_URL=http://localhost:41166
VITE_APP_ENV=dev

And my Laravel .env

APP_ENV=local
APP_CLIENT_URL=http://localhost:47344
APP_URL=http://localhost:41166
0 likes
2 replies
martinszeltins's avatar

a very dirty workaround is to temporarily change localhost to something like 192.168.1.100 - but that's far from perfect.

Please or to participate in this conversation.