How to create a sitemap using PHP & MySQL
Asked Answered
E

2

17

I was wondering how can I create a sitemap using PHP & MySQL and is there any sitemap design examples you know of?

Encaenia answered 25/10, 2010 at 21:13 Comment(3)
What do you mean by "sitemap" exactlyLikely
sitemap.xml for search engines? Or one for the user to view? If the latter, these things (mostly) went out of style in the last millennium.Ashurbanipal
one for search engines. thank you.Encaenia
W
20

I use this on my site, it works well and you can point Google's webmaster tools to "this_file.php" and it works wonders!

<?php
header("Content-type: text/xml");
echo'<?xml version=\'1.0\' encoding=\'UTF-8\'?>';
echo'   <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';

include '../include.php';
$sql = mysql_query("select blah from bleh");

while ($string = mysql_fetch_array($sql)){?>
            <url>
                <loc>http://www.domain.com/dir/<?echo $string['value'];?>/index.php</loc>
                <changefreq>weekly</changefreq>
            </url>
<?php } ?> 
</urlset>
Willena answered 2/2, 2011 at 3:51 Comment(4)
Will work for small sites. But for sites with huge user generated data, it will cry.Suggs
Lost lots of time trying this and then I see the php tag don´t contain <?php and my server only accept like this. Maybe be helpful to someone.Russelrussell
@RahulPrasad - caching the result works well for medium or large sites.Willena
Pretending XML is plain text? That's a paddling.Needy
C
2

The question is very vague -- we would need to know a lot more about the rest of your site before you'll get a good answer.

It depends on the structure of your page and what you want included in the map.

If your site is relatively static, then you should save your sitemap as a static page, so it doesn't cause extra processing every time its loaded. But if your site gets updated frequently, then you may need to refresh the map often, so a dynamic one that refreshes every time it is loaded may be better.

If your PHP site has a CMS structure and all your pages are included in the CMS, then it should be relatively simple to run through the database and pull out links to all your pages (depending, of course, on the structure of your CMS).

On the other hand, if your site is not structured in a way that lets you do this, or you want to limit the pages that are shown on the map, then you may find it easier to run a spider across the site and store the results.

There are plenty of existing site map programs already written. I googled php sitemap generator, and got a whole stack of results, of which quite a few look like they could be useful to you, even if only so you can download them to study their source code.

Cahn answered 25/10, 2010 at 21:25 Comment(1)
may I ask you to have a look at am xml sitemap related question here - tinyurl.com/pgqjdeo?Paleoclimatology

© 2022 - 2024 — McMap. All rights reserved.