magento2: Images added to sitemap are "404 Not Found" on Nginx [Magento 2.1.0]

Steps to reproduce

  1. Magento 2.1.0 and Nginx with https://github.com/magento/magento2/blob/2.1.0/nginx.conf.sample
  2. Go to Stores > Settings: Configuration > Catalog: XML Sitemap > Products Options
  3. Set Add Images into Sitemap to “All”
  4. Go to Marketing > SEO & Search: Site Map
  5. Add and generate sitemap

Expected result

  1. URL of images shouldn’t contain pub directory.
<image:image>
<image:loc>
https://example.com/media/catalog/product/s/a/sample-product-img.jpg
</image:loc>
<image:title>Sample product</image:title>
<image:caption>Sample product</image:caption>
</image:image>
<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0">
<DataObject type="thumbnail">
<Attribute name="name" value="Sample product"/>
<Attribute name="src" value="https://example.com/media/catalog/product/s/a/sample-product-img.jpg"/>
</DataObject>
</PageMap>

Actual result

  1. Images are “404 Not Found”
<image:image>
<image:loc>
https://example.com/pub/media/catalog/product/s/a/sample-product-img.jpg
</image:loc>
<image:title>Sample product</image:title>
<image:caption>Sample product</image:caption>
</image:image>
<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0">
<DataObject type="thumbnail">
<Attribute name="name" value="Sample product"/>
<Attribute name="src" value="https://example.com/pub/media/catalog/product/s/a/sample-product-img.jpg"/>
</DataObject>
</PageMap>

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 20 (11 by maintainers)

Most upvoted comments

Why hasn’t the issue been fixed in 2 years?

Still exist in 2.2.6 Backend generated sitemap.xml contains /pup/ in image src, when shops root is in /pup/.

Workaround by hostep works for me. Set generation time some minutes past to current time and generate sitemap with “bin/magento cron:run” generates sitemap without /pup/ in image path. Then I set frequency to monthly to not overwrite by automation.

I haven’t verified it it is still true on the latest version, but we definitely had the same problem with Magento 2.1.3 or 2.1.4 if I remember correctly.

We could work around it by setting the configurations:

  • General > Web > Base URLs > Base URL for User Media Files to {{unsecure_base_url}}media/
  • General > Web > Base URLs (Secure) > Base URL for User Media Files to {{secure_base_url}}media/

By default, these fields are empty and the ‘pub’ directory part was getting added to the image paths in the sitemap.xml files.

Be aware that you should test this with the ‘pub’ directory being used as docroot.

@IlnitskiyArtem: not sure if this helps? If you want I can test again using a plain vanilla Magento setup.

@IlnitskiyArtem Ok, I was able to reproduce this again in Magento 2.1.7 I forgot this bug was triggered by the cronjobs. If you generate the sitemap.xml file using the adminhtml, there is no problem, it only happens when you let the sitemap.xml file generate using the cronjobs.

So steps to reproduce:

  • Setup a clean Magento 2.1.7 shop and use ‘pub’ as docroot
  • Create one product with one image
  • Create a sitemap.xml, verify its content, there is no ‘pub’ in the image url
  • In the configuration, go to: Catalog > XML Sitemap > Generation Settings
  • Enable and put the start time one or two minutes from your current time
  • Save and flush cache
  • Manually run php bin/magento cron:run a couple of times until the generation of the sitemap.xml is triggered by this command
  • Check the sitemap.xml file again, it now contains ‘pub’ in the image url.

Would be great if this could be fixed! The workaround mentioned above gets us around the issue, but not all Magento customers will discover this workaround, so a fix would be appreciated 😃

Thanks!