Preconditions
app/etc/env.php
'directories' => [ 'document_root_is_pub' => true ],
'MAGE_MODE' => 'production',
document_root_is_pub
istrue
MAGE_MODE
is haveproduction
value
sitemap.xml
- Filename : sitemap.xml
- Path : /media/
Admin => Stores => Configuration => General => Web:
-
Base Urls => Base URL for User Media Files =>{{unsecure_base_url}}media/
-
Base URLs (Secure) => Secure Base URL for User Media Files => {{secure_base_url}}media/
But while generating the sitemap.xml via cron
Expected result
sitemap.xml
index urls to sitemap should be
https://magento-site/media/sitemap-1-1.xml
Actual result
sitemap.xml
urls look like
https://magento-site/pub/media/sitemap-1-1.xml
NOTE : When sitemap is generated from
admin
panelsitemap
contains the proper urls, But somehow withcron
it comes wrong url (containspub
in the url)
Most likely reason (Might be) But not in my case
Sometime problem happens when running the sitemap generation through
bin/magento cron:run from outside of the Magento directory.
- e.g our magento code is in /var/www/src, if I run php
src/bin/magento cron:run
from/var/www
the urls will contain src as part of the
baseurl. - Running from the magento directory, e.g
php bin/magento cron:run
from
/var/www/src
, works as expected. - But my cron is generating from
/var/www/src
- And Running from ADMIN UI – Site generate will also works as
expected.
Any solution or advise will be really helpful.
Best Answer
The reason is the way
getBaseUrl
function implemented onMagento\Store\Model\Store
.If you don't have a value set for
Base URL for User Media Files
(and/orSecure Base URL for User Media Files
) underStore > Settings > Configuration > General > Web > Base URLs
(and/orBase URLs (Secure)
) then$url = $this->getConfig($path);
will return null, causing the lastif
condition create the$url
.As it's seen, that if condition has a wrong implementation of combining base URL with media Directory.
When you are running your cronjob under
/var/www/src
, media directory is underpub/media/
. Because of the problematic part of the function, here how your base media URL gets created:Solution is to set;
Store > Settings > Configuration > General > Web > Base URLs > Base URL for User Media Files
value to{{unsecure_base_url}}media/
,Store > Settings > Configuration > General > Web > Base URLs > Secure Base URL for User Media Files
value to{{secure_base_url}}media/
,and refresh
Configuration
cache.