example.com and www.example.com domains
Hi guys, I have set APP_URL = https://example.com in my .env file
If I navigate the control panel with https://example.com/admin, when editing a record that has a FileUpload type field, the image is displayed correctly. If I browse with https://www.example.com/admin the image is no longer seen.
Is there a way to authorize the two domains (the one with and the one without www) in the .env file?
17 Replies
I believe the only way to handle this is at the web server level, so you have a single canonical URL, and everything else 301 redirects to that. Exactly how you do this depends on your web server. Apache uses htaccess. Nginx uses conf files.
Or if you don't want to mess with Apache/nginx vhosts, another option is to use any sort of a filesystem disk (S3, DO spaces etc) and save to disk. You'll have one consistent URL.
To be honest, to me anyway, this sounds like a DNS zone issue. They should both resolve to the same server just fine unless there is an explicit reason to not do it.
Afaik, It's not really a DNS thing, as all you can do in DNS is make sure that both canonical and non-canonical URL's arrive at the same place. You can't rewrite the host portion of the schema. So the web server is still going to see www.example.org or example.org, and set the hostname accordingly. You still need to rewrite that at the web server end, so one of them gets a 301 that redirects to the other, so the header gets rewritten, and any scripting that cares about APP_URL sees the one it expects.
Yea. It definitely involves both.
And the OP seems to have his DNS set up correctly, as he's able to hit the site with both versions.
Fair enough. Just the first thought that came to my mind.
If the servers servers then the same then it shouldn’t matter. You’re not wrong. Just a thought.
It's always good practice to handle having a canonical host rewrite at the web server level, partly to avoid issues like the OP's (and other similar issues, like CORS, XSS detection, etc), and partly for SEO (always having the non-canonical be a permanent redirect to the canonical, so you only have one hostyname for SEO).
No doubt, sometimes though it’s a pain in the ass when the tld is determined by the IT department. Lol.
Especially when the IT department doesn’t actually know what they are doing.
Or when you are trying to deal with the IT department as a contractor to some other department. 🙂
And neither department knows what they are doing.
@NeerGreeN Hugh is mostly like correct in this use case, but as far as laravel is concerned your app url in your env should point to the www version. That should take care or your issue, but you’ll need the server to force a redirect from non www to www subdomain.
Doesn't really matter which one you choose to be canonical, which is the one you put in your APP_ENV. Just matters that you 301 redirect the other one to it. Most sites these days seem to be choosing the naked domain as canonical, and www. is falling out of favor.
Thank you so much guys. The DNS points to the same server for both domains. I will set the rules in the .htaccess file. Thanks again
RewriteCond %{HTTP_HOST} ^www.hinostory.it [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://hinostory.it%{REQUEST_URI} [L,R=301]
I added these rules, but now I get a 500 error
I followed this guide and I don't want to have done any damage
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
With this command the 500 error was resolved: php artisan view:cache
I had to change the redirect: it must be from https://example.com to https://www.example.com otherwise in the administration panel, when I am editing a record, the images are not displayed in the FileUpload fields
Thanks again everyone