关键词:
文章目录
前言
首先是要交个人网站的详情页都提交给百度、360、神马、谷歌等搜索引擎。
如果是简单的列表页,当然可以写好在放到服务器目录下。
比如博客类动态生成的就必须每天跑脚本去追加写入了。
本文采用的是laravel框架的command命令来跑脚本,大家可以适合自己项目的写法。
什么是Sitemap?
Sitemap(即站点地图)就是您网站上各网页的列表。创建并提交Sitemap有助于百度发现并了解您网站上的所有网页。您还可以使用Sitemap提供有关您网站的其他信息,如上次更新日期、Sitemap文件的更新频率等,供百度Spider参考。
百度对已提交的数据,不保证一定会抓取及索引所有网址。但是,我们会使用Sitemap中的数据来了解网站的结构等信息,这样可以帮助我们改进抓取策略,并在日后能更好地对网站进行抓取。
此外,Sitemap 与搜索排名没有关系。
sitemap文件遵循指南
- 文本文件每行都必须有一个网址。网址中不能有换行。不应包含网址列表以外的任何信息。
- 您必须书写完整的网址,包括 http。
- 每个文本文件最多可包含 50,000 个网址,并且应小于10MB(10,485,760字节)。如果网站所包含的网址超过 50,000个,则可将列表分割成多个文本文件,然后分别添加每个文件。
- 文本文件需使用 UTF-8 编码或GBK编码。
xml格式详解
<?xml version="1.0" encoding="utf-8"?>
<!-- XML文件需以utf-8编码-->
<urlset>
<!--必填标签-->
<url>
<!--必填标签,这是具体某一个链接的定义入口,每一条数据都要用<url>和</url>包含在里面,这是必须的 -->
<loc>http://www.yoursite.com/yoursite.html</loc>
<!--必填,URL链接地址,长度不得超过256字节-->
<lastmod>2009-12-14</lastmod>
<!--可以不提交该标签,用来指定该链接的最后更新时间-->
<changefreq>daily</changefreq>
<!--可以不提交该标签,用这个标签告诉此链接可能会出现的更新频率 -->
<priority>0.8</priority>
<!--可以不提交该标签,用来指定此链接相对于其他链接的优先权比值,此值定于0.0-1.0之间-->
</url>
<url>
<loc>http://www.yoursite.com/yoursite2.html</loc>
<lastmod>2010-05-01</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
</urlset>
实战代码
sitemap组件代码
<?php
/**
* 通用生成sitemap文件
* User: WXiangQian
*/
namespace App\\Http\\Tools;
class CreateSiteMapXml
private $fileDir;
public function __construct($filePath)
$this->mkFolder($filePath);
$filePath = rtrim($filePath, '/') . '/';
$this->fileDir = $filePath;
/**
* 生成sitemaps 入口
* @param array $ary msps数组
* @param string $parent maps父标签
* @param string $type maps类型
* @return bool
*/
public function addSiteMap($ary, $parent, $type)
if (empty($ary))
return false;
$file = $this->fileDir . $type . ".xml";//获取最后更新文件
$content = $this->maps($ary, $parent); //生成片段 maps
$f = $this->readFile($file);
if (empty($f))
//获取新文件头部
$f = $this->typeMain();
else
$f = file_get_contents($file);
$nf = $this->strInsert($f, strpos($f, strrchr($f, '</')), $content);
$this->writeFile($file, $nf);
public function ary2maps($idAry, $loc, $cf, $pri)
$lastmod = date('Y-m-d');
$ary = array();
foreach ($idAry as $id)
$ary[] = array(
'loc' => $loc . $id,
'lastmod' => $lastmod,
'changefreq' => $cf,
'priority' => $pri,
);
return $ary;
//读取文件
public function readFile($file)
if (empty($file))
return false;
$f = file_exists($file);
return $f ?: '';
//写入文件
public function writeFile($fileName, $content)
//echo $content;
file_put_contents($fileName, $content); //更新其值
/**
* 通用 maps
* @param array $ary
* @param string $parent
* @return string
*
*/
public function maps($ary, $parent): string
$str = '';
if (is_array($ary))
foreach ($ary as $mval)
$str .= "<$parent>\\r\\n";
foreach ($mval as $key => $val)
$str .= " <$key>$val</$key>\\r\\n";
$str .= "</$parent>\\r\\n";
return $str;
/**
* 指定位置前插入字符串
* @param string $str 原字符串
* @param int $i 位置
* @param string $substr 插入的字符串
* @return string
*/
public function strInsert($str, $i, $substr): string
$lstr = substr($str, 0, $i);
$rstr = substr($str, $i, strlen($str));
$newstr = ($lstr . $substr . $rstr);
return $newstr;
// sitemap type
public function typeMain(): string
$xml = "<?xml version='1.0' encoding='UTF-8'?>\\r\\n<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' \\r\\n" .
"xmlns:mobile='http://www.sitemaps.org/schemas/sitemap-mobile/1'> \\r\\n" . "</urlset>";
return $xml;
// 检查目标文件夹是否存在,如果不存在则自动创建该目录
public function mkFolder($path)
if (!is_readable($path))
// is_file($path) or mkdir($path, 0777);
is_file($path) || mkdir($path, 0777) || is_dir($path);
调用sitemap组件
<?php
namespace App\\Console\\Commands;
use App\\Http\\Tools\\CreateSiteMapXml;
use Illuminate\\Console\\Command;
class CreateSiteMapXmlCommand extends Command
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'create_sitemap_xml';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成sitemap.xml文件';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
parent::__construct();
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
$idAry = [
117281450,
110532521,
105240627,
105558534,
];
$name = 'csdn';
// 服务器目录地址
$sitemaps = new CreateSiteMapXml('/Users/wxiangqian/my_project/laravel-api/sitemaps/');
$videoAry = $sitemaps->ary2maps($idAry, 'https://wxiangqian.blog.csdn.net/article/details/', 'daily', '0.8');
$sitemaps->addSiteMap($videoAry, 'url', $name);
仓库地址
https://github.com/WXiangQian/laravel-api
实战截图
相关问题
Sitemap提交后,多久能被百度处理?
Sitemap数据提交后,一般在1小时内百度会开始处理。处理完成的时间视文件大小和您设置的抓取周期而定。
提交的Sitemap都会被百度抓取并收录吗?
百度对已提交的数据,不保证一定会抓取及收录所有网址。是否收录与页面质量相关。
XML格式的 Sitemap 中,“priority”提示会影响我的网页在搜索结果中的排名吗?
不会。Sitemap 中的“priority”提示只是说明该网址相对于您自己网站上其他网址的重要性,并不会影响网页在搜索结果中的排名。
网址在 Sitemap 中的位置是否会影响它的使用?
不会。网址在 Sitemap 中的位置并不会影响百度对它的识别或使用方式。
Sitemap中提交的url能否包含中文?
因为转码问题建议最好不要包含中文。
结束语
希望本文可以帮助大家解决php生成sitemap.xml地图文件问题。👍
php生成sitemap.xml地图文件(代码片段)
文章目录前言什么是Sitemap?sitemap文件遵循指南xml格式详解实战代码sitemap组件代码调用sitemap组件仓库地址实战截图相关问题Sitemap提交后,多久能被百度处理?提交的Sitemap都会被百度抓取并收录吗?XML格式的Sitemap... 查看详情
php生成sitemap.xml地图文件(代码片段)
文章目录前言什么是Sitemap?sitemap文件遵循指南xml格式详解实战代码sitemap组件代码调用sitemap组件仓库地址实战截图相关问题Sitemap提交后,多久能被百度处理?提交的Sitemap都会被百度抓取并收录吗?XML格式的Sitemap... 查看详情
wordpress免插件生成完整站点地图(sitemap.xml)的php代码
让这个代码更加完善,可以同时生成首页、文章、单页面、分类和标签的sitemap!一、PHP代码<?phprequire(‘./wp-blog-header.php‘);header("Content-type:text/xml");header(‘HTTP/1.1200OK‘);$posts_to_show=1000;echo‘<?xmlversion="1.0"encoding="U 查看详情
网站地图sitemap.xml自动更新lastmod文件(php代码)-更新
上一遍《网站地图sitemap.xml自动更新lastmod文件(PHP代码)》代码在实际应用中并不理想,在浏览器中访问后,过一会就出现500的错误,不能实现夜间或者凌晨变更日期的目的。对代码进行了更新,把更新后的php页面在index.php中被... 查看详情
sitemap索引格式的网站地图怎么做
...等待,直到该网站是完全抓取。3,将被重定向到生成的Sitemap详细信息页,包括页数,断开的链接列表,XML文件的内容和链接到一个sitemap文件。使用链接下载的地图文件,并放入网站域名的根文件夹。4,转到站长工具(百度和... 查看详情
网站地图sitemap(代码片段)
...的收录概率。网站地图一般存放在域名根目录下并命名为sitemap,比如http://www.liujiangblog.com/sitemap.xml。一个典型的sitemap,其内 查看详情
nuxt.js做站点地图(sitemap.xml)详解(代码片段)
...在做跨境电商项目中seo是必须要做的,seo中站点地图(sitemap.xml,robots.txt)又是必不可少的,这里就记录一下nuxt中站点地图是如何做的。第一步:安装@/nuxt/sitemapnpminstall@nuxtjs/sitemap第二步:在根目录static目录下新建sitemap.j... 查看详情
nuxt.js做站点地图(sitemap.xml)详解(代码片段)
...在做跨境电商项目中seo是必须要做的,seo中站点地图(sitemap.xml,robots.txt)又是必不可少的,这里就记录一下nuxt中站点地图是如何做的。第一步:在根目录static目录下新建sitemap.jssitemap.xml文件的内容importaxiosfrom"axios";const... 查看详情
sitemap怎样生成?
...那个被百度,谷歌,雅虎,爱问收录那种。谢谢!!googlesitemap怎样生成呢?谢谢~~sitemap怎么生成1.利用site生成器生成sitemap文件,新建文件——输入域名(例如”www.fuyeor.com“),文件存储位置——进入生成页面,点击开始——文... 查看详情
什么是sitemap?
sitemap是什么?sitemaps是网站地图的意思。网站地图是为便于搜索引擎抓取和用户更方便地找到自己需要的内容而设计的,它分两种,分别用于用户导航和搜索引擎收录:1、sitemap.xml格式网站地图sitemap.xml格式网站地图为:方便搜... 查看详情
网站地图sitemap.xml样式代码sitemap.xsl(代码片段)
网站地图sitemap.xml样式代码sitemap.xsl。对原有sitemap.xsl代码样式进行了修改,直接上源码。mark<?xmlversion="1.0"encoding="UTF-8"?><xsl:stylesheetversion="2.0"xmlns:html="http://www.w3.org/TR/REC-html40"xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"xml... 查看详情
django网站地图sitemap(代码片段)
...的收录概率。网站地图一般存放在域名根目录下并命名为sitemap,比如http://www.liujiangblog.com/sitemap.xml。一个典型的sitemap,其内容片段如下:ThisXMLfiledoesnotappeartohaveanystyleinformationassociatedwithit.Thedocumenttreeisshownbelow.<urlsetxmlns="http://... 查看详情
htaccess - 将所有文件重写为 index.php,除了 sitemap.xml
】htaccess-将所有文件重写为index.php,除了sitemap.xml【英文标题】:htaccess-Rewriteallfilestoindex.phpexceptsitemap.xml【发布时间】:2018-04-0306:31:48【问题描述】:我正在尝试将每个php文件重定向到index.php,sitemap.xml除外。问题是sitemap.xml也... 查看详情
生成多sitemap文件
Thinkphp生成多sitemap文件 我们知道sitemap对于seo的重要性,很多介绍只生成一个文件sitemap.xml,但是如果网站内容比较多,就要生成多个sitemap文件,因为搜索引擎对sitemap文件大小和条数有限制,比如google对每个sitemap文件的... 查看详情
帝国cms怎么制作sitemap.xml和baidumap.xml网站地图
参考技术A直接使用帝国cms提供的sitemap插件,官网上有的,你搜下 查看详情
xml更新robots.txt以重定向到sitemap.xml(代码片段)
帝国cms怎么制作sitemap.xml和baidumap.xml网站地图
...择直接页面,页面名称为:网站地图,文件名修改为../../sitemap.xml '?> [!--news.url--] daily 1.0 [!--news.url--]hmoban daily 0.8 [!--news.url--]wpyuanma daily 0.8 [!--news.url--]diguoCMS daily 0.8 [!--ne... 查看详情
baidusitemapgenerator伪静态如何使用
...maccessingthroughHTTP#RulestoensurethatnormalcontentgetsthroughRewriteRule/sitemap.xml/sitemap.xml[L]RewriteRule/favicon.ico/favicon.ico[L]#Forfile-basedwordpresscontent(i.e.theme),admin,etc.RewriteRule/wp-(.*)/wp-$1[L]#Fornormalwordpresscontent,viaindex.phpRewriteRule^/$/index.php[L]RewriteRule/(... 查看详情