Tuesday, 6 June 2017

Make Dynamic XML sitemap in PHP Script



If you want to create dynamic sitemap for your website in PHP, then you have come very right place, because in this tutorial we have make discussion on how can we create XML Sitemap for our dynamic website which is created by using PHP Script. We have already know for create XML Sitemap for static site, there is lots of online tools available for creating XML sitemap for static site. But here we have creating dynamic XML sitemaps by using PHP Script.

Here we have made discussion on how to create a sitemap for PHP based website. So first of all what is Sitemap in simple terms. XML Sitemap is a series of webpage URL which can be accessible by search engine crawlers and users. Based on Sitemap search engine will index website page on their search engine. So If you want to index your website web page on Search engine then you have to create sitemap for your website. But here we want to create dynamic sitemap for our PHP website, that means when we have add new web page in our website, then that webpage link must be automatically added into sitemap.

For Create XML Sitemap for PHP website, then first of all you have to store web page url into one table, so we can easily fetch URL which we want to added into sitemap. After fetching of all web page URL then after we want to convert into XML format. So by using header() function we can convert page data into XML sitemap format. So when we have run this page then it will produce XML Sitemap with .php extension. But in all search engine sitemap extension must in .xml format. So We have use Apache .htaccess configuration file. In this file we have define URL Rewrite-rule which will be redirect .php URL to .xml URL. So, this we have make dynamic PHP XML sitemap by using PHP script.




Source Code


sitemap.php



<?php 
//sitemap.php
$connect = mysqli_connect("localhost", "root", "", "testing");

$query = "SELECT page_url FROM page";

$result = mysqli_query($connect, $query);

$base_url = "http://localhost/tutorial/php-sitemap/";

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;

while($row = mysqli_fetch_array($result))
{
 echo '<url>' . PHP_EOL;
 echo '<loc>'.$base_url. $row["page_url"] .'/</loc>' . PHP_EOL;
 echo '<changefreq>daily</changefreq>' . PHP_EOL;
 echo '</url>' . PHP_EOL;
}

echo '</urlset>' . PHP_EOL;

?>


.htaccess



RewriteEngine On

RewriteRule ^sitemap\.xml/?$ sitemap.php


Database



--
-- Database: `testing`
--

-- --------------------------------------------------------

--
-- Table structure for table `page`
--

CREATE TABLE IF NOT EXISTS `page` (
  `page_id` int(11) NOT NULL,
  `page_title` text NOT NULL,
  `page_url` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `page`
--

INSERT INTO `page` (`page_id`, `page_title`, `page_url`) VALUES
(1, 'JSON - Dynamic Dependent Dropdown List using Jquery and Ajax', 'json-dynamic-dependent-dropdown-list-using-jquery-and-ajax'),
(2, 'Live Table Data Edit Delete using Tabledit Plugin in PHP', 'live-table-data-edit-delete-using-tabledit-plugin-in-php'),
(3, 'Create Treeview with Bootstrap Treeview Ajax JQuery in PHP\r\n', 'create-treeview-with-bootstrap-treeview-ajax-jquery-in-php'),
(4, 'Bootstrap Multiselect Dropdown with Checkboxes using Jquery in PHP\r\n', 'bootstrap-multiselect-dropdown-with-checkboxes-using-jquery-in-php'),
(5, 'Facebook Style Popup Notification using PHP Ajax Bootstrap\r\n', 'facebook-style-popup-notification-using-php-ajax-bootstrap'),
(6, 'Modal with Dynamic Previous & Next Data Button by Ajax PHP\r\n', 'modal-with-dynamic-previous-next-data-button-by-ajax-php'),
(7, 'How to Use Bootstrap Select Plugin with Ajax Jquery PHP\r\n', 'how-to-use-bootstrap-select-plugin-with-ajax-jquery-php'),
(8, 'How to Load CSV File data into HTML Table Using AJAX jQuery\r\n', 'how-to-load-csv-file-data-into-html-table-using-ajax-jquery'),
(9, 'Autocomplete Textbox using Typeahead with Ajax PHP Bootstrap\r\n', 'autocomplete-textbox-using-typeahead-with-ajax-php-bootstrap'),
(10, 'Export Data to Excel in Codeigniter using PHPExcel\r\n', 'export-data-to-excel-in-codeigniter-using-phpexcel');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `page`
--
ALTER TABLE `page`
  ADD PRIMARY KEY (`page_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `page`
--
ALTER TABLE `page`
  MODIFY `page_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=11;

24 comments:

  1. I have a Statics Website Solar panel Manufacturers how to create dynamic sitemap for a static website?

    ReplyDelete
    Replies
    1. No need to create dynamic sitemap for static website. static means you already know all the url of your website so just include them in your sitemap. Dynamic sitemap are for dynamic websites wher url and pages are created dynamicaly.

      Delete
  2. Thank you so much for sharing your code. i wish best for you.

    ReplyDelete
  3. thankyou soo much for making sitemap creation so easy. this really means alot. thankyou once again :)

    ReplyDelete
  4. i have site https://sexincest.online/ want to see its normal or not

    ReplyDelete
  5. If I write URL domain.com/something-any-word website is running properly. But I need a sitemap in XML format so I write PHP code in sitemap.php. I want in runtime sitemap.xml

    my current .htaccess file is working
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

    but i write in this your suggested RewriteRule website not work. How to solve it? Please guide me.

    ReplyDelete
  6. Hello Sir,

    This PHP Scripts something error in hosting thats for not working please help me

    This is error URL link - https://prnt.sc/s6yho3

    Please sir help me!!

    ReplyDelete
  7. Thank You! Very Much Sir

    Can you please tell me How it use full for SEO?

    ReplyDelete
  8. Go to the https://www.xml-sitemaps.com/
    Submit your Website URL
    It will automatically generate sitemap.
    Download it & upload to your root folder
    & submit sitemap uRL to Search Console

    ReplyDelete
  9. Thanks a lot. It solved my problem. Thankyou.

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. Whereis the script to update database? I think its not dynamic

    ReplyDelete
  12. Thank you. Although i have a problem setting it up on code igniter. I mean using controllers

    ReplyDelete
  13. Thanks alot, it really work for me

    ReplyDelete
  14. can you please tell me what editor are you using?

    ReplyDelete