Rails /sitemap.xml 重定向到根目录

Rails /sitemap.xml redirects to root

我正在使用 dynamic_sitemaps gem 生成站点地图。 https://github.com/lassebunk/dynamic_sitemaps

在本地,当我尝试访问 localhost/sitemap.xml 时,它会显示站点地图文件。

当我在 vps 的生产环境中尝试此操作时,它不起作用,只是将我重定向到 root_path。

如果我 cd 到 public/sitemaps/ 我可以看到 sitemap.xml(它存在)。

这里有什么问题吗?

这是我的 routes.rb

Rails.application.routes.draw do
  .....

  get 'sitemap' => 'home#sitemap', format: :xml
  get 'robots' => 'home#robots', format: :text#, defaults: { format: :text }
  get '*path', to: 'application#handle_404'
end

我试过删除 format: :xml 但没有成功

/robots.txt 工作正常。

这是来自家庭控制器的站点地图方法

  def sitemap
    respond_to do |format|
      format.xml { render file: 'public/sitemaps/sitemap.xml' }
      format.html { redirect_to root_url }
    end
  end

handle_404 在应用程序控制器中

  def handle_404
    redirect_to root_path
  end

public/sitemaps...

开头添加/后问题解决
render file: '/public/sitemaps/sitemap.xml