sitemap php script

A script like the following can generate automatically your sitemap (in xml format):

<?php
function genera_sitemap($path) {
  $sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  $sitemap .= '<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">' . "\n";

  // Scansiona le directory e sottodirectory del sito
  $dir = './';
  $files = scandir($dir);
  foreach ($files as $file) {
    if ($file != '.' && $file != '..' && strpos($file, '.') !== 0) {
      if (is_dir($dir . $file)) {
        // Scansiona la sottodirectory
        $sitemap .= aggiungi_sottodirectory($dir . $file . '/', $path) . "\n";
      } else {
        // Aggiungi il file alla sitemap
        if (preg_match('/\.php$/', $file) && $file != 'sql-pass.php') {
          $sitemap .= '<url>' . "\n";
          $sitemap .= ' <loc>' . $path . '/' . $file . '</loc>' . "\n";
          $sitemap .= ' <lastmod>' . date('c', filemtime($dir . $file)) . '</lastmod>' . "\n";
          //$sitemap .= ' <changefreq>daily</changefreq>' . "\n";
          $sitemap .= ' <priority>1.0</priority>' . "\n";
          $sitemap .= '</url>' . "\n";
        }
      }
    }
  }

  $sitemap .= '</urlset>' . "\n";

  // Salva la sitemap su file
  $file = 'sitemap.xml';
  $fp = fopen($file, 'w');
  fwrite($fp, $sitemap);
  fclose($fp);
}

function aggiungi_sottodirectory($dir, $path) {
  $sitemap = '';
  $files = scandir($dir);
  foreach ($files as $file) {
    if ($file != '.' && $file != '..' && strpos($file, '.') !== 0) {
      if ($file == 'index.php') {
        $priority = '1.0';}
      elseif (preg_match('/testi/', $file)) {
          $priority = 0.2;
        }
      else {
        $priority = 0.6;
        }
      if (is_dir($dir . $file)) {
        // Scansiona la sottodirectory
        $sitemap .= aggiungi_sottodirectory($dir . $file . '/', $path) . "\n";
      } else {
        // Aggiungi il file alla sitemap
        if (preg_match('/\.php$/', $file)) {
          $sitemap .= '<url>' . "\n";
          $sitemap .= ' <loc>' . $path . substr($dir, 2) . $file . '</loc>' . "\n";
          $sitemap .= ' <lastmod>' . date('c', filemtime($dir . $file)) . '</lastmod>' . "\n";
          //$sitemap .= ' <changefreq>daily</changefreq>' . "\n";
          $sitemap .= ' <priority>' . $priority . '</priority>' . "\n";
          $sitemap .= '</url>' . "\n";
        }
      }
    }
  }
  return $sitemap;
}
?>

You can call it from different websites, with a code like the following (assuming that your script is genera-sitemap-commune.inc:

<?php
$path="https:[your-desired-path]";
include "genera-sitemap-commune.inc";
genera_sitemap($path);
?>

Lascia un commento

Il tuo indirizzo email non sarĂ  pubblicato. I campi obbligatori sono contrassegnati *