PHP Help please?

+1 Gavin Lee · December 28, 2014
How would I get this to index pages that have no ending at all? My website is created of pages similar to this and because it has no ending it does not display within the sitemap.

define( 'SITEMAP_DIR', './' );
define( 'SITEMAP_DIR_URL', '' );
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' );


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 ) )

    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

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

<urlset xmlns=""><?php

I am not interested in rewriting the urls because I have already done this from the when I first started.

Post a Reply


Oldest  Newest  Rating
0 Chris Sisco · December 28, 2014
I've never messed around with sitemaps before but I did find something that might help you. IF your posts are stored in a database,
0 Gavin Lee · December 28, 2014

This is the error I am getting with this code that you provided in the url.

Not personally familiar with the error so I am a little lost not going to lie.
The line is this.

header('Content-type: application/xml');

Also within my config it looks a little something like this for database settings maybe something is wrong there but I am going to assume that it's connecting to the database as the error says nothing about a database connection.

$servername = "Host";
$username = "Username";
$password = "Password";

Thanks in advance.
0 Chris Sisco · December 28, 2014
Like I said I have no experience with sitemaps. From what I've read on this error though is that it's an error with your XML.

If I were trying to deal with this problem I would use the original Sitemap.php file that you posted above since it already mostly works and just add the code to it that will get the post information you need from the database.


<urlset xmlns=""><?php

//I would put the code to pull post information from the database here.
Create a loop and get the information needed from each post and echo in the same format that gets generated in the function that's already there.
<loc><?php echo $url . rawurlencode( $file ); ?></loc>
<lastmod><?php echo $mod; ?></lastmod>
<changefreq><?php echo $chfreq; ?></changefreq>
<priority><?php echo $prio; ?></priority>

0 Raphael de Oliveira · January 2, 2015
@Gavin Murphy

Upload the broken XML (the one you've shown the picture of the error) on pastebin so we can take a look
  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.