You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让基于Sitemap.ejs路由的站点地图适配Google Search Console?

Fixing HTML Sitemap Issue in Google Search Console

Looks like you’re hitting a common snag when using EJS to generate sitemaps—Google’s rejecting it because it’s being served as HTML instead of proper XML. Here’s how to fix it in two key steps:

1. Update Your Route to Serve the Correct Content Type

EJS defaults to rendering content with a text/html Content-Type header, which tells Google it’s a web page instead of a sitemap. You need to override this in your server code (assuming you’re using Express):

app.get('/sitemap.xml', (req, res) => {
  // Set the header to XML first
  res.set('Content-Type', 'application/xml');
  // Pass in your dynamic data (e.g., list of page URLs, last modified dates)
  res.render('sitemap', { 
    pages: [
      { url: 'https://yourdomain.com/', lastModified: '2024-05-20', changeFreq: 'weekly', priority: 1.0 },
      { url: 'https://yourdomain.com/about', lastModified: '2024-04-15', changeFreq: 'monthly', priority: 0.8 }
      // Add all your pages here
    ]
  });
});

Pro tip: Name your route /sitemap.xml (with the .xml suffix) instead of just /sitemap—it helps Google immediately recognize what it’s dealing with.

2. Rewrite Your Sitemap.ejs Template as XML

Your current EJS template is probably outputting HTML tags (like <html>, <body>) which break the XML structure. Replace it with a valid sitemap XML template:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <% pages.forEach(page => { %>
    <url>
      <loc><%= page.url %></loc>
      <lastmod><%= page.lastModified %></lastmod>
      <changefreq><%= page.changeFreq %></changefreq>
      <priority><%= page.priority %></priority>
    </url>
  <% }) %>
</urlset>

Make sure there are no leftover HTML elements in this template—even a stray <div> will cause Google to reject it. EJS’s <%= %> tag will automatically escape special characters (like & or <) in your URLs, which is crucial for valid XML.

Final Checks

  • Visit https://yourdomain.com/sitemap.xml in your browser, right-click > "View Page Source"—you should see pure XML, no HTML.
  • Use Google Search Console’s Sitemap Testing Tool to validate your sitemap before resubmitting—it’ll flag any syntax errors.
  • Double-check the response headers (using browser dev tools > Network tab) to confirm Content-Type: application/xml is being sent.

Once you’ve made these changes, resubmit the sitemap in Google Search Console—it should be recognized correctly!

内容的提问来源于stack exchange,提问作者Bruno Pigatto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:13:58