12

Didn't catch this on my local machine but I've noticed some of my pages returning the naked IP address of my web server instead of the domain name. Example:

route('homepage') will sometimes return 192.XX8.X.2XX or 192.XX8.X.2XX/index.php or domain.com/index.php. My pages are cached upon the first visit and there's a rough 50% chance the things will come out weird for all URLs on the page.

Is there an explanation for this weird behavior and how would I fix it? It's quite a concern since Googlebot is listing three additional duplicate copies of my site.

Note: I'm also using the LaravelLocalization package for my routes: https://github.com/mcamara/laravel-localization

I'm also running this app under Laravel Forge (Nginx)

Helen Che
  • 1,951
  • 5
  • 29
  • 41
  • You could write htaccess rules to redirect the IP address to the domain name. Google shouldnt cache a 301/302 redirecting page. – chris85 May 07 '15 at 06:32
  • Thanks, I'll try that as a temporary solution but I still need to fix/investigate `route()` issue. – Helen Che May 07 '15 at 06:34
  • what have you got for APP_URL in the .env variable? – Terry Mar 18 '19 at 09:23
  • I'm a bit late to the party, but I have the exact same issue. Did you find a solution in the meantime? – gregory Jul 31 '19 at 20:47
  • @gregory seems like there is a bug, or maybe intentionally laravel does not use APP_URL inside route generation. Inside route URL generator where absolute url is being built, laravel uses symfony getBaseUrl which returns request base url and not whats inside your APP_URL. So basically if you cache URLs and some bot will reach your server via IP address you will end up with ip based URLs and the only solution is to manage cache in such a way that cache is domain specific. :D This is stupid, but I dont see any other solution. – Vytenis Ščiukas Jul 13 '20 at 17:30

3 Answers3

4

I understand, the question is very old, but I've had same problem and found the solution. (it can help someone)

Open file RouteServiceProvider on this path: app/Providers/RouteServiceProvider.php

Look for function mapWebRoutes. Now we have to add caller domain with config from our environment

protected function mapWebRoutes()
{
    Route::middleware('web')
        ->namespace($this->namespace)
        ->domain(\Config::get('app.url', null))
        ->group(base_path('routes/web.php'));
}

During building the url based on route - system tries to get local variable domain in current route, if it found - the domain should apply immediately. But if property undefined - go to Symfony route module.

PS: Don't forget to set APP_URL in your .env file. Example:

APP_URL=https://mysimpledomain.ua
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Vasyl Zhuryk
  • 1,228
  • 10
  • 23
1

I had gread success using

\URL::forceRootUrl(\Config::get('app.url'));

in RouteServiceProvider.php on boot().

Felix D.
  • 4,811
  • 8
  • 38
  • 72
-2

This is most likely down to your nginx configuration. If you only have your app configured on the server then it will act as the default. When you visit the server directly by it's IP address (or any other domain that links to your server) then nginx will serve the laravel app.

There are many approaches to deal with this. I would suggest to either configure a default for the server. This could be just a html page saying "congratulations you've reached the server" or something similar. The other is just reconfigure you app's nginx file to redirect anything that's not the correct domain to the domain name for you app.

server {
    if ($host != example.com) {
        return 301 https://example.com$request_uri;
    }
    listen 80;
    server_name example.com;
}

More about how nginx processes a request here: http://nginx.org/en/docs/http/request_processing.html

forsvunnet
  • 1,238
  • 10
  • 17
  • Laravel should handle this, as it should use APP_URL for building urls, I constantly end up with wrong urls, because I use cache and some bot reaches my website by IP, ofc I can change my server configuration, but I want to know why Laravel does not handle this. – Vytenis Ščiukas Jul 13 '20 at 17:20