Building Dynamic Sitemap Using PHP

One day you might discover that some of your websites are not showing up in the search results. You might believe they have links to them somewhere on your website, but you do not know for sure. Unless the search engines have banned that web page, odds are that particular page does not have any links to it from the rest of the website. Honestly, you should correct this by providing a proper navigation structure for your users, but sometimes that is not a feasible option especially with a mountain of content.

Even with the super crawling powers of search engines, web pages can often hidden for one reason or another. To reduce the amount of your hidden web pages not found in the search results, you should create a sitemap. I do not mean that you should go search for some website that will make the sitemap for you. Think about it, if Google cannot find your pages, what makes you think that the website sitemap generator website can? You know your website better than anyone else does, and maybe you even have access to all of the files. Therefore, you are the most suited to create the website sitemap for your users and the search engines.

Sitemaps prefer to be in XML format, but that does not mean that you have to write XML code for each page manually. Let’s build a XML sitemap together with PHP.

header("Content-Type: application/xml; charset=utf-8"); 
echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 
echo '<urlset xmlns="" xmlns:xsi="" xsi:schemaLocation="">' .PHP_EOL; 
function urlElement($url) {
echo '<url>'.PHP_EOL; 
echo '<loc>'.$url.'</loc>'. PHP_EOL; 
echo '<changefreq>weekly</changefreq>'.PHP_EOL; 
echo '</url>'.PHP_EOL;
echo '</urlset>'; 

In the example, we immediately send a header to inform the browser that the content type is XML and that it should render the page as XML. After that, we set up the version information and where the schema is located, but you can just copy all of that. The interesting part is our urlElement function that takes the url as an argument and creates a url element in our sitemap. I use the only use the changefreq and loc as a child element of url. However, you can also use lastmod and priority to your url elements if you want. I believe both of elements are useless as search engines determine these elements on their own.

If you happen to have a dynamic website with content in a database, you can create a function that loops through each url to that content. All you need to do is place the content’s url inside the urlElement call during each loop. Feel free to modify and build an XML sitemap with the code above as a foundation to create dynamic sitemaps. You might think you have to submit XML files to Google, Bing, or other search engines, but you would be wrong. They happily accept any type of file as long as it outputs an XML file. Personally, I use a modification of this XML sitemap generator to loop through my content and then add individual pages to construct the entire sitemap for my website.

(Post Source : &
Akhil Trivedi