Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating dynamic sitemap with php

Tags:

php

xml

sitemap

First let me tell you I am learning php. I want to create an automatically dynamic sitemap for my site. The script (suppose sitemap.php) will crawl all the links of my site and will create an array of 1000 urls. Once 1000 urls limit reaches, it will make an another page id with 1000 urls array. So that the script output will be like this :

<sitemapindex xmlns="http://www.google.com/schemas/sitemap/0.84" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/siteindex.xsd">

  <sitemap>
    <loc>http://exmaple.com/sitemap.php?page=1</loc>
  </sitemap>
  <sitemap>
    <loc>http://example.com/sitemap.php?page=2</loc>
  </sitemap>
</sitemapindex>

Page 1 will have 1000 urls in xml format and page 2 will have also and this will go on as the site is updated.I have tried so many codes from github and other seach pages from google. But have not found exactly what I need. Can you help please.

like image 445
A K Pal Avatar asked Jun 08 '26 05:06

A K Pal


1 Answers

A good solution is to have access to you root folder and add to your apache .htaccess file the following line after RewriteEngine On

RewriteRule ^sitemap\.xml$ sitemap.php [L]

and then simply having a file sitemap.php in your root folder that therefore would be normally accessible via http://yoursite.com/sitemap.xml, the default URL where all search engines will firstly search.

The file sitemap.php shall start with, to make sure that the server sends the correct HTTP header:

<?php header('Content-type: application/xml; charset=utf-8') ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
like image 137
João Pimentel Ferreira Avatar answered Jun 10 '26 19:06

João Pimentel Ferreira