How to: Created XML Sitemap Using PHP

This articles inspired to share from my experience when I need to created custom sitemap in XML format for some of my website. Basically we will using PHP to generated URL following the sitemap in XML format. Why should in XML format? because mostly a great search engine using sitemap with XML format as their standard.

So we will using PHP script to generated URL (others will follows standard XML format). Example we have site using PHP and MYSQL we will need to query data from MYSQL and then echo it as URL. The problem is because not all website using same structure I can not give you working scripts to generate sitemap as the result may not working for your site.

The good news, because mostly all website using same method (ex, PHP+mysql blabla) we can use same method to generated sitemap. First we need to make sure the first tag should be like this…

On the second line here comes your job to generated your site URL to listed. The last thing just the closing tag. If you meet problems just read the protocols for sitemap XML standard from here. Learn about each tag function and implement it as needed.


Next step, after we have PHP script that works to generated sitemap we need to make it looks like static sitemap. This trick can be done from your .htacess file by rewrite sitemap.php to become sitemap.xml, Just put this line on your .htacess file (if you don’t have it created one).

RewriteEngine on
RewriteRule ^sitemap.xml$ sitemap.php [L]

Done, it will rewrite sitemap.php to become sitemap.xml and looks like static sitemap. Next step, This sitemap will shown as XML in internet explorer but not in mozilla firefox so we need to declare it as XML. Just put this code on the first line..

header(“Content-Type: text/xml; charset=utf-8”)

Next, If you have more than 5,000 URL you can generated it and saved each sitemap and giving parent sitemap.

The result will shown like this sample:

In this sample I’m using PHP function to read folder and files because basically my site using folder and files to generated links. In another sample I’m using mysql query to generated my sitemap.

If yours company need someone to making scripts like this I can do it for $100 – $10,000 deppend on how hard to coding it. That’s my share for today, sorry for not blogging over a month. I promised will write more in the future.

Have a nice day 🙂

Similar Posts:


Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.