Creating an automatic sitemap.xml with PHP

+2 Gavin Lee · December 27, 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' );
// 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' );

With the script displayed above I show the basic format in php

Post a Reply


Oldest  Newest  Rating
0 Jay Deshaun · September 13, 2015
I will have to look at this another time, looks interesting.
+1 Alex . · December 29, 2014
I will try and explain so its understandable, i have a website with nine million pages. This is how i did it.

Basically i store all my urls in a database so when a new page is created the new url is stored. It also assigns the same row an incrementing number because an xml sitemap can only handle 50,000 urls so you have to break them up. I did it by groups of 5000 just to help with load times.  

I will give you my code. If you cant understand it msg me. :)

<?php header('Content-type: application/xml');?>

include 'connect.php';

$first = 5000 * 9 + 1;

$second = $first + 5000;
$result = mysqli_query($con,"SELECT * FROM table WHERE numrows BETWEEN $first AND $second LIMIT 5000");

while($runrows = mysqli_fetch_array($result))
 $id = $runrows['id'];
 $title = $runrows['title'];
 $link = $runrows['link'];
 $updated = $runrows['updated'];

$timestamp = strtotime($updated);
$date = date("Y-m-d", $timestamp);
   echo "<url><loc>$link</loc><lastmod>$date</lastmod><changefreq>yearly</changefreq></url>

0 Gavin Lee · December 28, 2014
Yes exactly @Alex 

You could re code the entire thing if you felt fit.. but I don't think it's required.
How ever if you took the time I would be extremely greatfull :) 
0 Alex . · December 28, 2014
Im a bit confused, so you want to generate a xml sitemap using php that adds new post urls as you or someone posts them?


<urlset xsi:schemaLocation="">
<url><loc>New post url here</loc>

0 Gavin Lee · December 28, 2014
Nobody can help? :( 

This makes indexing pages which generate 200 - 300 post a day extremely hard.. and nothing online says anything about it to help me.
  • 1



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