如何在sitemap.xml中将主URL设置为优先级1.0,将子目录URL优先级设置为0.5

时间:2015-04-28 22:57:42

标签: php xml sitemap sitemap.xml

如何使站点地图中的根索引优先级为1.0,其余子目录文件的优先级为0.5?

我正在使用这个PHP代码在访问页面时动态生成我的站点地图。

就像现在一样,它给出了设定值

的优先级

$url -> appendChild( $priority = $xml->createElement('priority', '0.5') );

对于站点地图中的每个网址,如下例所示:

sitemap example

创建站点地图的PHP代码:

<?
$ignore = array( 'images', 'css', 'includes', 'cgi-bin', 'xml-sitemap.php' );

  function getFileList($dir, $recurse=false) {
  global $ignore;

    $retval = array();

    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while ( false !== ( $entry = $d->read() ) ) {

        // Check if this dir needs to be ignored, if so, skip it.
        if ( in_array( utf8_encode( $entry ), $ignore ) )
            continue;

      // skip hidden files
      if($entry[0] == ".") continue;
      if(is_dir("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry/",
          "type" => filetype("$dir$entry"),
          "size" => 0,
          "lastmod" => filemtime("$dir$entry")
        );
        if($recurse && is_readable("$dir$entry/")) {
          $retval = array_merge($retval, getFileList("$dir$entry/", true));
        }
      } elseif(is_readable("$dir$entry")) {
        $retval[] = array(
          "name" => str_replace('./','/', $dir),
          "type" => mime_content_type("$dir$entry"),
          "size" => filesize("$dir$entry"),
          "lastmod" => filemtime("$dir$entry")
        );
      }
    }
    $d->close();

    return $retval;
  }

  $dirlist = getFileList("./", true);  

    // sitemap creation
    $time  = time();
    $sitemap = $_SERVER['DOCUMENT_ROOT'].'/sitemap.xml';
    if ($time - filemtime($sitemap) >= 1) { // 1 days

        $xml = new DomDocument('1.0', 'utf-8'); 
        $xml->formatOutput = true; 

        // creating base node
        $urlset = $xml->createElement('urlset'); 
        $urlset -> appendChild(
            new DomAttr('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
        );

            // appending it to document
        $xml -> appendChild($urlset);

        // building the xml document with your website content
        foreach($dirlist as $file)
        {
        if($file['type'] != 'text/x-php') continue;
            //Creating single url node
            $url = $xml->createElement('url'); 

            //Filling node with entry info
            $url -> appendChild( $xml->createElement('loc', 'http://www.airsahara.in'.$file['name']) ); 
            $url -> appendChild( $lastmod = $xml->createElement('lastmod', date('Y-m-d', $file['lastmod'])) ); 
            $url -> appendChild( $changefreq = $xml->createElement('changefreq', 'monthly') ); 
            $url -> appendChild( $priority = $xml->createElement('priority', '0.5') ); 

            // append url to urlset node
            $urlset -> appendChild($url);

        }
        $xml->save($sitemap);
    } // if time



    ?>

0 个答案:

没有答案