rogervila
rogervila

Reputation: 1054

Laravel route works, but returns 404 status

I have created a dynamic sitemap.xml route with laravel, that returns a xml response.

The route works on the browser, but it returns a 404 status.

This is the only route that returns a 404 status

These are the headers:

Cache-Control →no-cache
Connection →keep-alive
Content-Length →232
Content-Type →text/xml; charset=UTF-8
Date →Thu, 24 Mar 2016 09:44:35 GMT
Server →nginx/1.9.12

This is the route:

Route::get('sitemap.xml', ['as' => 'sitemap.index', 'uses' => 'SitemapController@index']);

This is the controller response:

    $response = response()->view('sitemaps.index', [
        'last'    => $data,
        'modules' => $modules,
        'app'     => $app,
    ])->header('Content-Type', 'text/xml');

    $response->header('Content-Length',strlen($response->getOriginalContent()));

    return $response;

The view:

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach($modules as $module)
    <sitemap>
        <loc>{{ url('sitemap-' . $module . '.xml') }}</loc>
        <lastmod>{{ $last[$module]['updated_at'] or date('Y').'-01-01'}}</lastmod>
    </sitemap>
@endforeach

Thank you.

Upvotes: 2

Views: 1314

Answers (1)

rogervila
rogervila

Reputation: 1054

It was a Nginx problem.

I added this code on my config file and now it works:

location = /sitemap.xml {
    try_files $uri $uri/ /index.php?$query_string;
    access_log off;
    log_not_found off;
}

And I removed the <?xml version="1.0" encoding="UTF-8"?> from the response.

Upvotes: 1

Related Questions