简体   繁体   中英

Creating dynamic sitemap with php

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.

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"?>' ?>

One solution would be to create a php script that generates a static sitemap.xml with all the website urls. After that, run the php script periodically using a CRON job (for example once a week). This will update the sitemap automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM