Mike Mike - 7 months ago 31
PHP Question

How do I set main URL a priority of 1.0 and subdirectory urls priority of 0.5 in sitemap.xml

How can I make the root index in the sitemap a priority of 1.0 and the rest of the sub directory files a priority of 0.5?

I'm using this php code to generate my sitemap on the fly when page is accessed.

As it is right now, it gives a priority of whatever the set value is

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


for every url in the sitemap like in this example:

sitemap example

PHP code that creates sitemap:

<?
$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



?>

Answer

I'm answering my own question because I figured it out.

I simply did this. I took this line of code and expanded it with an if and else statement.

went from 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: As per the comments of hakre

        if ($file['name'] != '/') {
            $p = '0.5';
            } else {
                $p = '1.0';
            }
        $url -> appendChild( $priority = $xml->createElement('priority', $p) );

And this would be the shorthand of the if else statement

        $file['name'] != '/' ? $p = '0.5' : $p = '1.0';
        $url -> appendChild( $priority = $xml->createElement('priority', $p) );