php - How do I set main URL a priority of 1.0 and subdirectory urls priority of 0.5 in sitemap.xml -
how can make root index in sitemap priority of 1.0 , rest of sub directory files priority of 0.5?
i'm using php code generate sitemap on fly when page accessed.
as right now, gives priority of whatever set value is
$url -> appendchild( $priority = $xml->createelement('priority', '0.5') );
for every url in sitemap in example:

php code creates sitemap:
<? $ignore = array( 'images', 'css', 'includes', 'cgi-bin', 'xml-sitemap.php' ); function getfilelist($dir, $recurse=false) { global $ignore; $retval = array(); // open pointer directory , read list of files $d = @dir($dir) or die("getfilelist: failed opening directory $dir reading"); while ( false !== ( $entry = $d->read() ) ) { // check if dir needs 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 document $xml -> appendchild($urlset); // building xml document website content foreach($dirlist $file) { if($file['type'] != 'text/x-php') continue; //creating single url node $url = $xml->createelement('url'); //filling node 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 urlset node $urlset -> appendchild($url); } $xml->save($sitemap); } // if time ?>
i'm answering own question because figured out.
i did this. took line of code , expanded if , else statement.
went this
$url -> appendchild( $priority = $xml->createelement('priority', '0.5') ); to this
if($file['name'] != '/') { $url -> appendchild( $priority = $xml->createelement('priority', '0.5') ); } else { $url -> appendchild( $priority = $xml->createelement('priority', '1.0') ); } update: per comments of hakre
if ($file['name'] != '/') { $p = '0.5'; } else { $p = '1.0'; } $url -> appendchild( $priority = $xml->createelement('priority', $p) ); and shorthand of if else statement
$file['name'] != '/' ? $p = '0.5' : $p = '1.0'; $url -> appendchild( $priority = $xml->createelement('priority', $p) );
Comments
Post a Comment