Sitemap

The site's /sitemap.xml lists the pages search engines should know about. yf builds it from the sitemap hooks of the user modules: a module with a public _hook_sitemap($sitemap) (or _hook_site_map(), _site_map_items()) adds its URLs, and the generator (the user module site_map) writes them to files under uploads/site_map/, one set per language, and serves them for SITEMAP_LIVE_TIME (12 hours) before the next request writes them again. The framework's news, static_pages and faq have hooks (the shop plugin brings its own). Generation is off until site_map.SITE_MAP_ENABLED is on (Settings, tab SEO); the web server routes /sitemap.xml to the sitemapxml module, which answers an empty sitemap on every host that is not in main()->PRODUCTION_DOMAIN.

Adding URLs from a module

public function _hook_sitemap($sitemap)
{
    foreach ((array) db()->from('articles')->where('active', '1')->get_all() as $a) {
        $sitemap->_add([
            'url' => '/articles/show/' . (int) $a['id'], // a path goes through url(); an absolute URL is kept
            'last_update' => (int) $a['edit_date'],      // lastmod: a timestamp or a date string
        ]);
    }
    return true;
}

A string item is just the URL. priority and changefreq may be given too (search engines mostly ignore them; DEFAULT_PRIORITY and DEFAULT_CHANGEFREQ fill them in). With site_map.EXTENSIONS on, an item may also carry images (URLs), videos (thumbnail_loc, title, description, content_loc or player_loc), news (name, language, publication_date, title) and alternates (['en' => url, 'de' => url, 'x-default' => url], the hreflang versions of the page). Leave out pages behind a login, pages with noindex and URLs whose canonical points elsewhere. A hook that returns an array of items works too. Which modules run is site_map.MODULES_TO_INCLUDE (empty: every active module with a hook) minus site_map.MODULES_EXCLUDE (names or globs).

Settings and switches

All on the Settings page, tab SEO (the Sitemap page shows them with their source). Besides the module lists, the defaults and the limits (MAX_ENTRIES URLs and MAX_SIZE bytes per file, SITEMAP_LIVE_TIME, USE_LOCKING), five switches change the output, each off so that a site keeps its sitemap byte for byte until it turns one on:

  • site_map.FILES_FIX: the URLs are XML-escaped (a URL with & made the old file invalid XML), relative ./? URLs made absolute, several files joined by a sitemap index (before, only the first file was served), files written under a temporary name and renamed when complete, only the language being generated replaced, and a request that finds another generation running gets the previous file.
  • site_map.HTTP_HEADERS: application/xml; charset=UTF-8, Last-Modified with a 304 answer to If-Modified-Since, Cache-Control for the rest of the cache lifetime and X-Robots-Tag: noindex (the XML file itself stays out of search results; search engines still read it).
  • site_map.GZIP: gzip to clients that accept it (a .gz copy next to the file); not needed when the web server compresses XML.
  • site_map.LASTMOD_FROM_DATA: the framework's hooks give the real date of their content as lastmod (news: its date).
  • site_map.EXTENSIONS: image, video, news and hreflang entries of hook items, with their namespaces.

robots.txt names the sitemap with robotstxt.SITEMAP_LINE (see SEO). NOTIFY_GOOGLE pings an endpoint Google retired in 2023; the doctor asks to turn it off.

site_map.ALLOW_REWRITE (set in $PROJECT_CONF['site_map'], only while tpl.REWRITE_MODE is on) never rewrote anything: a ./?object=... URL even stayed relative. Turn on site_map.REWRITE_FIX with it and each <loc> in this site's query form (./?object=news&action=show&id=7, WEB_PATH?object=...) goes through url_user() and gets the pretty URL; other URLs stay as they are, and a ./? URL without object is made absolute. Off (the default) keeps the old output; REWRITE_SPLIT_LENGTH applies to the old branch only. Hooks that already call url() get rewritten URLs without either switch.