Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling.
In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL like when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site so that search engines can more intelligently crawl the site.
So, in this tutorial we will show you how to create sitemap using PHP.You may also like create rss feed using PHP.
To Create XML Sitemap It Takes Only One Step:-
- Make a PHP file to create sitemap
Step 1. Make a PHP file to create sitemap
We make a PHP file and save it with a name sitemap.php
// Sample Structure Of XML Sitemap <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.example.com/index.php </loc> <lastmod>2016-01-23T18:00:15+00:00</lastmod> </url> </urlset> // Database Structure CREATE TABLE 'post' ( 'link' text NOT NULL, 'date' text NOT NULL, ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 <?php header('Content-type: application/xml'); $host="localhost"; $username="root"; $password=""; $databasename="sample"; $connect=mysql_connect($host,$username,$password); $db=mysql_select_db($databasename); $get_result=mysql_query("select * from post"); echo "<?xml version='1.0' encoding='UTF-8'?>"."\n"; echo "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>"."\n"; echo " <url> <loc>http://talkerscode.com/</loc> <lastmod>2016-01-23T18:00:15+00:00</lastmod> <changefreq>daily</changefreq> </url> <url> <loc>http://talkerscode.com/about.php</loc> <lastmod>2016-01-23T18:00:15+00:00</lastmod> <changefreq>daily</changefreq> </url> <url> <loc>http://talkerscode.com/contact.php</loc> <lastmod>2016-01-23T18:00:15+00:00</lastmod> <changefreq>daily</changefreq> </url>"; while($row=mysql_fetch_array($get_result)) { echo "<url>"; echo "<loc>".$row['link']."</loc>"; echo "<lastmod>".$row['date']."</lastmod>"; echo "<changefreq>daily</changefreq>"; echo "</url>"; } echo "</urlset>"; ?>
In this step we create a database called 'post' and insert our post link and date created and then write proper header to display xml file in browser.
Then we write structure of sitemap and get all the post link and date and insert in our sitemap after that we display the sitemap and copy all the text from browser and create a file 'sitemap.xml' and insert the copied text to it.
You may also like read xml file using PHP.
That's all, this is how to create xml sitemap using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
Latest Tutorials
- Create Animated Skill Bar Using jQuery, HTML And CSS
- Simple And Best Responsive Web Design Using CSS Part 2
- Show Button On Hover Using HTML And CSS
- Material Design Login Form Using jQuery And CSS
- Animated Progress Bar Using jQuery And CSS
- Dynamic Drop Down Menu Using jQuery, Ajax, PHP And MySQL
- Get Website Statistics Using PHP, jQuery And MySQL
- HTML5 Login And Signup Form With Animation Using jQuery
- Facebook Style Homepage Design Using HTML And CSS
- View Webpage In Fullscreen Using jQuery