首页 > 解决方案 > 如何在 Rails 上正确创建站点地图

问题描述

我按照这个来创建我的站点地图。

所以在config/sitemap.rb我有

require 'fog-aws'

SitemapGenerator::Sitemap.default_host = "https://www.lapenderiedelhomme.fr"

SitemapGenerator::Sitemap.create do
  add clients_products_path, :priority => 0.5, :changefreq => 'daily'

  Product.find_each do |prod|
    add clients_product_path(prod),:lastmod => prod.updated_at, :priority => 0.5
  end

end

SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new(fog_provider: 'AWS',
                                                                    aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
                                                                    aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY_ID'],
                                                                    fog_directory: ENV['S3_BUCKET_NAME'],
                                                                    fog_region: 'eu-west-3')

SitemapGenerator::Sitemap.public_path = 'tmp/'
SitemapGenerator::Sitemap.sitemaps_host = "https://mybucket.s3.eu-west-3.amazonaws.com/"
SitemapGenerator::Sitemap.sitemaps_path = 'sitemap/'

SitemapGenerator::Sitemap.ping_search_engines('https://lapenderiedelhomme.fr/sitemap')

在我添加的路线中

get '/sitemap', to: 'clients/pages#sitemap'

在我的客户/pages_controller.rb

我有这个方法:

def sitemap
    redirect_to 'https://mybucket.s3.eu-west-3.amazonaws.com/sitemap/sitemap.xml.gz'
end

public/robot.txt

User-Agent: *
Sitemap: https://www.lapenderiedelhomme.fr/sitemap

将所有内容推送到 Heroku 后,我运行以下命令:

heroku run rails sitemap:create --app lapenderie-production

我有这个结果

Running rails sitemap:create on ⬢ lapenderie-production... up, run.6997 (Hobby)
/app/config/initializers/meta_tags.rb:1: warning: already initialized constant DEFAULT_META
/app/config/initializers/default_meta.rb:1: warning: previous definition of DEFAULT_META was here
In '/app/public/':
D, [2019-06-30T16:36:05.675507 #4] DEBUG -- :   Product Load (19.3ms)  SELECT  "products".* FROM "products" ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
+ sitemap.xml.gz                                          57 links /    1.23 KB
Sitemap stats: 57 links / 1 sitemaps / 0m00s

Pinging with URL 'https://lapenderiedelhomme.fr/sitemap':
  Successful ping of Google
  Successful ping of Bing

但是当我访问这个链接时https://lapenderiedelhomme.fr/sitemap

我有这个:

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>5DF520C4759DD7A2</RequestId>
<HostId>
v2jM40g++QrfdB5TLtpo6hAwkshUkP6jZbgLXgozOyjuoEX8KlRBvnGaVqE9DzccuR81zZUN+pY=
</HostId>
</Error>

sitemap我在我的存储桶中创建了一个读写文件夹......

标签: ruby-on-railsrubyseositemap

解决方案


您的 S3 存储桶是公开的吗?您必须授权公共访问才能正常工作。


推荐阅读