laraveldnshost

Using one Laravel app from different domains - building urls issue


I searched for my issue in so many ways, but I don't seem to find the correct case, so I'm asking here.

I have a Laravel app which is installed on a server and everything works correct. The domain is set as HTTP only and is configured from AWS. However we need to have another domain which should work only from HTTPS. The HTTP domain is pointing to the server instance and the HTTPS one is pointing to a CloudFront distribution with origin the HTTP domain. The issue is that when I open the HTTPS domain, all of the links and images are loaded from the HTTP domain.

To be more concrete, let's say I have http://mysite-notsecure.example.com and https://mysite-secure.example.com. When I open http://mysite-notsecure.example.com everything works as it should and there are no issues. However when I open https://mysite-secure.example.com the site loads, also files like app.js and app.css load with the correct host, but things like fonts, images, links, etc, load from http://mysite-notsecure.example.com.

Because most of the urls are built with the url() function, I think the issue has something to do with APP_URL, which was first set to http://mysite-notsecure.example.com, but when I added the new domain, I set it to empty (APP_URL=), however the urls are still built the same way (I cleared config cache). What should I do in order for my site to build the urls according to the current host? I don't need any other change for the two domains. They should load everything exactly the same, only the host should remain and not redirect to the other domain.


Solution

  • It turned out there were two different issues. I'll describe them here, because there is a slight chance someone could be dealing with one of them.

    First, I printed the contents of the $_SERVER variable on both domains and the host in both was the same - the HTTP domain. This issue was from the CloudFront configuration. Turned out the Host header was removed from the CF distribution behavior, so that CF replaced it with the origin's value (the origin is the http domain). After this was fixed, the host in $_SERVER appeared correctly.

    But the initial issue for the urls building was something else which I didn't think of. After clearing the cache to remove the debugging and seeing the right urls on the HTTPS domain, I switched back to the HTTP one and saw now there all the urls pointed to the HTTPS domain. That is when it hit me that these domains share not only the configuration, but also the cache. And most of my urls on the page I was testing with, were coming from a function with cache, so when the cache was stored from one of the domains, they appeared the same on the other. When I included the host in the cache key, everything worked correctly.

    Hope this helps someone else.