[Script PHP] Creare una sitemap XML
Creare manualmente una sitemap con estensione XML utilizzando uno script PHP
La sitemap è la mappa del sito contenente la lista delle pagine e delle cartelle contenute all’interno del sito.
Essa è importantissima per Google, infatti aiuta certamente l’indicizzazione di buona parte delle pagine segnalate.
In questa guida ti mostrerò come generare una sitemap con estensione .xml utilizzando uno script in PHP, questo script preleverà tutti i files di un intero dominio e le stamperà nel file .xml.
PASSO 1 – Copia lo script
Per prima cosa devi copiare il seguente codice ed inserirlo in una pagina con estensione .php (es: crea-sitemap.php) :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Sitemap</title> </head> <body> <h1>Generatore di Sitemap con estensione .xml</h1> <p> <?php // GESTISCE GLI URL DELLE DIRECTORY - NON MODIFICARE $dir = "./"; // MODIFICA LA VARIABILE dominio INSERENDO IL TUO DOMINIO $dominio = "http://www.NOMESITO.it"; // NOME DEL FILE CHE SARÀ AGGIORNATO. LA PRIMA VOLTA CREA UN FILE DI TESTO VUOTO E SALVALO COME NOME "NOMEFILE".XML $filexml = "sitemap.xml"; // I TIPI DI FILE CHE VUOI INCLUDERE NELLA GOOGLE SITEMAP $type = array ( "htm" , "html" , "php" ); // DICHIARAZIONE DELLA FREQUENZA DI AGGIORNAMENTO: never, hourly, daily, weekly, monthly, yearly, always $changefreq = "weekly"; // Dichiari la priorità da 0.1 a 1.0 $priority = "0.5"; // FINE EDIT AREA // NON MODIFICARE DA QUI IN POI echo "Document Root: ".$_SERVER['DOCUMENT_ROOT']."<br/>"; echo "Current Dir: ".str_replace("\\", "/" ,getcwd())."<br/>"; echo "Differenza: ".str_replace($_SERVER['DOCUMENT_ROOT'],"",str_replace("\\", "/" ,getcwd()))."<br/>"; $subdir = str_replace($_SERVER['DOCUMENT_ROOT'],"",str_replace("\\", "/" ,getcwd())); /* $header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<urlset 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/sitemap.xsd\">"; */ $header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\">\n"; $footer = "</urlset>\n"; function read_recursiv( $path ) { $result = array(); $handle = opendir ( $path ); if($handle) { while(false!==($file=readdir($handle))) { if ($file!="." && $file!="..") { $name = $path."/".$file; if(is_dir($name)) { $ar = read_recursiv ( $name ); foreach($ar as $value) { $result[] = $value; } } else { $result[] = $name; } } } } closedir($handle); return $result; } $data = read_recursiv ( $dir ); $sitemap = fopen($filexml, "w"); fwrite($sitemap, $header); foreach($data as $value) { $value = str_replace($dir, "", $value); $temp2 = strtolower(substr($value, strlen($value)-2, strlen($value))); $temp3 = strtolower(substr($value, strlen($value)-3, strlen($value))); $temp4 = strtolower(substr($value, strlen($value)-4, strlen($value))); if((in_array($temp2, $type)) || (in_array($temp3, $type)) || (in_array($temp4, $type))) { fwrite ( $sitemap , "<url>\n<loc>".$dominio.$value."</loc>\n<lastmod>".date("Y-m-d", filemtime($_SERVER['DOCUMENT_ROOT'].$subdir.$value))."</lastmod>\n<changefreq>".$changefreq."</changefreq>\n<priority>".$priority."</priority>\n</url>\n" ); echo $dominio.$value."<br/>"; } } fwrite ( $sitemap , $footer ); fclose ( $sitemap ); echo "<br/>Google Sitemap creata con successo"; ?></p> </body> </html> |
Devi modificare:
- $dominio = “http://www.NOMESITO.it”;
inserisci l’url del tuo sito - $type = array ( “htm” , “html” , “php” );
Indica le estensioni dei file che vuoi includere nella sitemap
PASSO 2 – Crea il file sitemap.xml
Nel tuo dominio, crea un file con nome ed estensione “sitemap.xml”.
Questo file deve essere completamente vuoto.
PASSO 3 – Genera la sitemap
Dopo aver creato i files crea-sitemap.php e sitemap.xml, in entrambi i files troverai la lista dei files del tuo sito.
Fonte: googlerank.com
Articoli simili:
-
#
Scritto da Come inviare una sitemap a Google | SenzaWeb.it il 17-09-2010 alle ore 16:26
[...] Crea una sitemap tramite uno script PHP [...]
-
#
Scritto da Giancarlo Piccinini il 17-03-2011 alle ore 03:56
ho provato il suo script per generare sitemap.xml ma mi ha dato errore sotto
Chiedo aiuto Grazie
———————————————-
Generatore di Sitemap con estensione .xmlDocument Root: /web/htdocs/www.mio-sito.com/home/
Current Dir: /web/htdocs/www.mio-sito.com/home
Differenza: /web/htdocs/www.mio-sito.com/homeFatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 32768 bytes) in /web/htdocs/www.mio-sito.com/home/crea-sitemap.php on line 54
-
#
Scritto da Realizzazione siti il 20-07-2011 alle ore 12:32
Lo script ha dato molti errori anche a me, avete per caso rivisto il codice?
-
#
Scritto da siti web il 04-12-2011 alle ore 09:27
Script molto interessante in php…
Provato e perfettamente funzionante!

