【问题标题】:Laravel route works, but returns 404 statusLaravel 路由有效,但返回 404 状态
【发布时间】:2016-07-11 21:10:23
【问题描述】:

我用 laravel 创建了一个动态的 sitemap.xml 路由,它返回一个 xml 响应。

路由在浏览器上运行,但它返回404状态

这是唯一返回 404 状态的路由

这些是标题

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

这是路线

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

这是控制器响应

    $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;

观点

<?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

谢谢。

【问题讨论】:

    标签: php xml laravel nginx http-status-code-404


    【解决方案1】:

    这是一个 Nginx 问题。

    我在我的配置文件中添加了这段代码,现在它可以工作了:

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

    我从回复中删除了&lt;?xml version="1.0" encoding="UTF-8"?&gt;

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2018-03-08
      • 2021-07-13
      • 1970-01-01
      • 2013-07-27
      • 2020-08-26
      相关资源
      最近更新 更多