我能够在我的开发环境中构建sitemap.xml,但是在将其推送到生产环境时,我不断收到此错误。
XML Parsing Error: mismatched tag. Expected: </link>.
Location: https://www.sample.com/sitemap.xml
Line Number 68, Column 4: </head>
---^
在控制器中,我喜欢这样:
def sitemap
headers['Content-Type'] = 'application/xml'
respond_to do |format|
format.xml
end
end
视图就像这样(sitemap.xml.builder):
xml.instruct! :xml, :version=>'1.0'
xml.tag! 'urlset', "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
if Rails.env.production?
base_url = "https://#{request.host_with_port}"
else
base_url = "http://#{request.host_with_port}"
end
xml.tag! "link", rel: 'alternate', hreflang: "en", href: base_url
xml.url do
xml.loc base_url
xml.lastmod "2015-01-01"
xml.changefreq "yearly"
xml.priority 1.0
end
xml.url do
xml.loc "#{base_url}/intro"
xml.lastmod "2016-04-03"
xml.changefreq "monthly"
xml.priority 1.0
end
end
生成的XML:
<urlset>
<url>
<loc>http://localhost:3001</loc>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>http://localhost:3001/blogs</loc>
<lastmod>2016-04-03</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>http://localhost:3001/sample_links/blog1</loc>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
</urlset>
我已经解决了这个问题,有一个代码错误,其中某些字段为空并导致错误。