使用php进行自动更新站点地图

时间:2014-12-27 18:21:08

标签: php

我如何将这个索引到完全没有结尾的页面?我的网站是由与此http://www.example.com/example/post-id-the-post-title-would-go-here类似的页面创建的,因为它没有结尾,所以它不会显示在站点地图中。

更新

的config.php

define( 'SITEMAP_DIR', './' );
define( 'SITEMAP_DIR_URL', 'http://www.example.com' );
define( 'RECURSIVE', true );
$filetypes = array( 'php', 'html', 'pdf' );
// The replace array, this works as file => replacement, so 'index.php' => '', would make the index.php be listed as just /
$replace = array( 'index.php' => '' );
$xsl = 'xml.xsl';
$chfreq = 'daily';
$prio = 1;
$ignore = array( 'config.php' );

我对重写网址不感兴趣,因为我刚开始时已经从http://www.exaple.com/index.php?a=track&id=17完成了此操作。

Sitemap.php

require './config.php';

// Get the keys so we can check quickly
$replace_files = array_keys( $replace );

// Sent the correct header so browsers display properly, with or without XSL.
header( 'Content-Type: application/xml' );

echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";

$ignore = array_merge( $ignore, array( '.', '..', 'config.php', 'xml-sitemap.php' ) );

if ( isset( $xsl ) && !empty( $xsl ) )
echo '<?xml-stylesheet type="text/xsl" href="' . SITEMAP_DIR_URL . $xsl . '"?>' . "\n";

function parse_dir( $dir, $url ) {
global $ignore, $filetypes, $replace, $chfreq, $prio;

$handle = opendir( $dir );
while ( false !== ( $file = readdir( $handle ) ) ) {

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

    if ( is_dir( $file ) ) {
        if ( defined( 'RECURSIVE' ) && RECURSIVE )
            parse_dir( $file, $url . $file . '/' );
    }

    // Check whether the file has on of the extensions allowed for this XML sitemap
    $fileinfo = pathinfo( $dir . $file );
    if ( in_array( $fileinfo['extension'], $filetypes ) ) {

        // Create a W3C valid date for use in the XML sitemap based on the file modification time
        if (filemtime( $dir .'/'. $file )==FALSE) {
            $mod = date( 'c', filectime( $dir . $file ) );
        } else {
            $mod = date( 'c', filemtime( $dir . $file ) );
        }

        // Replace the file with it's replacement from the settings, if needed.
        if ( in_array( $file, $replace ) )
            $file = $replace[$file];

        // Start creating the output
    ?>

    <url>
    <loc><?php echo $url . rawurlencode( $file ); ?></loc>
    <lastmod><?php echo $mod; ?></lastmod>
    <changefreq><?php echo $chfreq; ?></changefreq>
    <priority><?php echo $prio; ?></priority>
    </url><?php
    }
    }
closedir( $handle );
}

?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><?php
parse_dir( SITEMAP_DIR, SITEMAP_DIR_URL );
?>

</urlset>

0 个答案:

没有答案