帮助中心

Sitemap对于搜索引擎的收录很有用,所以每个网站需要一个sitemap.xml,那么标准格式是什么呢?用程序如何来自动生成呢?下面小孚给大家分享一下。

Sitemap的格式是什么?如何用程序生成站点地图

2020-02-18

Sitemap对于搜索引擎的收录很有用,所以每个网站需要一个sitemap.xml,那么标准格式是什么呢?用程序如何来自动生成呢?下面小孚给大家分享一下。

sitemap.xml文件的标准格式

<?xml version="1.0" encoding="utf-8"?><!-- XML文件需要使用utf-8编码--><urlset><!--这个标签是必须的-->    <url>        <!--也是一个必填标签,这是具体某一个链接的定义入口,每一条数据都要用<url>和</url>包含在里面 -->        <loc>https://www.zzuf.com/news/1.html</loc>        <!--必填,具体的链接地址,长度不得超过256字节,如果里面有特殊符号,需要转义(如:&转义为&amp;)-->        <lastmod>2020-01-01</lastmod>        <!--选填标签,用来指定该链接的最后更新时间,可以是日期也可以是日期加时间-->        <changefreq>daily</changefreq>        <!--选填标签,链接的更新频率:always ,hourly ,daily ,weekly ,monthly ,yearly ,never  -->        <priority>0.8</priority>        <!--选填标签,用来指定此链接相对于其他链接的优先权比值,此值定于0.0-1.0之间-->    </url>    <url>        <loc>https://www.zzuf.com/news/2.html</loc>        <lastmod>2020-02-01</lastmod>        <changefreq>daily</changefreq>        <priority>0.8</priority>    </url></urlset>

用PHP来生成sitemap.xml

$sql = "SELECT * FROM " . $ZZUF->table('article') . " ORDER BY id DESC";$thread = $ZZUF->query($sql)->fetchall();// 创建一个DOMDocument对象$dom = new DOMDocument("1.0","utf-8");header("Content-Type: text/xml");// 创建根节点$root = $dom->createElement("urlset");$dom->appendChild($root);foreach($thread as $key => $row){// 建立根下子节点track$track = $dom->createElement("url");$root->appendChild($track);// 建立track节点下元素$loc = $dom->createElement("loc");$track->appendChild($loc);$priority = $dom->createElement("priority");$track->appendChild($priority);$lastmod = $dom->createElement("lastmod");$track->appendChild($lastmod);$changefreq = $dom->createElement("changefreq");$track->appendChild($changefreq);// 获取到域名$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://": "http://";$url = $protocol . $_SERVER['HTTP_HOST'];// 赋值$text = $dom->createTextNode($url.'/news/'.$row["id"].'.html');$loc->appendChild($text);$date = date("Y-m-d",time());$text = $dom->createTextNode($date);$lastmod->appendChild($text);$text = $dom->createTextNode(daily);$changefreq->appendChild($text);$text = $dom->createTextNode(0.8);$priority->appendChild($text);}//生成xml文件,路径根据自己情况而定$dom->save("sitemap.xml");exit(json_encode(array("state" => 1, "msg" => "新的Sitemap已经生成")));


如没特殊注明,文章均为友孚原创,转载请注明来自:https://www.zzuf.com/news/7.html