go top

MEGA Sale

  • Colection of 65 PHP scripts for $6 each

Building dynamic sitemap using PHP

Limited offer!

Get a huge collection of PHP scripts in a special bundle!

Mega Sale Price
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.

<?php 
header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
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">' .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;
}

urlElement('http://www.afterhoursprogramming.com/services/default-text-generator/');
urlElement('http://www.afterhoursprogramming.com/tests/practice/');
urlElement('http://www.afterhoursprogramming.com/tests/practice/HTML/');
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.

Special Offer!

Get all 65 PHP scripts in a bundle for $6 each only!

Mega Sale Offer

5 Comments to "Building dynamic sitemap using PHP"

  • Dipesh

    Dipesh

    March 13, 2018 at 08:08 am

    Wich file add this code?

  • Nipun

    Nipun

    August 24, 2017 at 20:18 pm

    How can i add date in above code??
    Just Like This :

    echo '<url>'.PHP_EOL;
    echo '<loc>'.$url.'</loc>'. PHP_EOL;
    echo '<lastmod> '.$pdt.' </lastmod>'. PHP_EOL;
    echo '<changefreq>Daily</changefreq>'.PHP_EOL;
    echo '</url>'.PHP_EOL;

    Please answer ASAP !!

  • sumit

    sumit

    February 9, 2017 at 09:12 am

    Thanks for this code this is working...............

  • dc

    dc

    May 5, 2014 at 14:06 pm

     echo date("Y-m-d"); 

  • Ravi

    Ravi

    December 12, 2013 at 15:08 pm

    I have copied the php code which you are showing above and past it in my php file pleas tell me what is the file name i have to keep
    and I just need to upload it in server ?

    Thank you

Add your comment

Captcha
    • Free Scripts

      Add great new functionalities to your website with our Free Scripts collection.

      Free scripts
    • PHP Scripts

      Check our extensive collection of top-notch PHP Scripts that will enhance your website!

      Commercial PHP scripts