Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New way of doing config, some minor fixes.

  • Loading branch information...
commit ce157d15bda5c6e93784224ddf4a3de396ac5703 1 parent 6ff0818
@jdevalk authored
Showing with 72 additions and 62 deletions.
  1. +4 −0 README.markdown
  2. +28 −0 config-sample.php
  3. +40 −62 xml-sitemap.php
View
4 README.markdown
@@ -5,3 +5,7 @@ This simple PHP script is meant to help you easily create XML Sitemaps for stati
## Configuration
Open the PHP file and configure the settings until it says "STOP EDITING HERE". It's that simple.
+
+## Changelog
+
+2012-09-29: move configuration to config.php, fix outputted URLs
View
28 config-sample.php
@@ -0,0 +1,28 @@
+<?php
+/**
+ * Change the configuration below and rename this file to config.php
+ */
+
+// The directory to check, this script doesn't work recursively. Make sure the DIR ends ups in the Sitemap URL Dir below, otherwise the links to files will be broken!
+define( 'SITEMAP_DIR', './' );
+
+// With trailing slash!
+define( 'SITEMAP_DIR_URL', 'http://www.example.com/' );
+
+// The file types, you can just add them on, so 'pdf', 'php' would work
+$filetypes = array( 'php', 'html', 'pdf' );
+
+// The replace array, this works as file => replacement, so 'index.php' => '', would make the index.php be listed as just /
+$replace = array( 'index.php' => '' );
+
+// The XSL file used for styling the sitemap output, make sure this path is relative to the root of the site.
+$xsl = 'xml-sitemap.xsl';
+
+// The Change Frequency for files, should probably not be 'never', unless you know for sure you'll never change them again.
+$chfreq = 'never';
+
+// The Priority Frequency for files. There's no way to differentiate so it might just as well be 1.
+$prio = 1;
+
+// Ignore array, all files in this array will be: ignored!
+$ignore = array( 'config.php' );
View
102 xml-sitemap.php
@@ -1,83 +1,61 @@
-<?php
+<?php
/**
* XML Sitemap PHP Script
* For more info, see: http://yoast.com/xml-sitemap-php-script/
- * Copyright (C), 2011 - Joost de Valk, joost@yoast.com
+ * Copyright (C), 2011 - 2012 - Joost de Valk, joost@yoast.com
*/
-// The directory to check, this script doesn't work recursively
-define( 'SITEMAP_DIR', './');
+require './config.php';
-// The file types, you can just add them on, so 'pdf', 'php' would work
-$filetypes = array( 'php', 'html', 'pdf' );
-
-// The replace array, this works as file => replacement, so 'index.php' => '', would make the index.php be listed as just /
-$replace = array( 'index.php' => '' );
-
-// The XSL file used for styling the sitemap output, make sure this path is relative to the root of the site.
-$xsl = '/xml-sitemap.xsl';
+// Get the keys so we can check quickly
+$replace_files = array_keys( $replace );
-// The Change Frequency for files, should probably not be 'never', unless you know for sure you'll never change them again.
-$chfreq = 'never';
+// Sent the correct header so browsers display properly, with or without XSL.
+header( 'Content-Type: application/xml' );
-// The Priority Frequency for files. There's no way to differentiate so it might just as well be 1.
-$prio = 1;
+echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
-// Ignore array, all files in this array will be: ignored!
-$ignore = array( 'config.php' );
+if ( isset( $xsl ) && !empty( $xsl ) )
+ echo '<?xml-stylesheet type="text/xsl" href="'. SITEMAP_DIR_URL . $xsl . '"?>' . "\n";
-/**
- * STOP EDITING HERE. (UNLESS YOU KNOW WHAT YOU'RE DOING)
- */
+?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+ <?php
-// Get the keys so we can check quickly
-$replace_files = array_keys( $replace );
+ // Open the dir that was asked for.
+ if ( $handle = opendir( SITEMAP_DIR ) ) {
+ while ( false !== ( $file = readdir( $handle ) ) ) {
+ // Check if this file needs to be ignored, if so, skip it.
+ if ( in_array( $file, $ignore ) )
+ continue;
-// Sent the correct header so browsers display properly, with or without XSL.
-header('Content-Type: application/xml');
+ // Check whether the file has on of the extensions allowed for this XML sitemap
+ $fileinfo = pathinfo( SITEMAP_DIR . $file );
+ if ( in_array( $fileinfo['extension'], $filetypes ) ) {
-echo '<?xml version="1.0" encoding="utf-8"?>'."\n";
+ // Create a W3C valid date for use in the XML sitemap based on the file modification time
+ $mod = date( 'c', filemtime( SITEMAP_DIR . $file ) );
-if ( isset( $xsl ) && $xsl != '' )
- echo '<?xml-stylesheet type="text/xsl" href="http://'.$_SERVER['SERVER_NAME'].$xsl.'"?>'."\n";
+ // Replace the file with it's replacement from the settings, if needed.
+ if ( in_array( $file, $replace_files ) )
+ $file = $replace[$file];
-?>
-<urlset 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" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><?php
+ // Start creating the output
+ ?>
- // Open the dir that was asked for.
- if ( $handle = opendir( SITEMAP_DIR ) ) {
- while ( false !== ( $file = readdir($handle) ) ) {
- // Check if this file needs to be ignored, if so, skip it.
- if ( in_array( $file, $ignore ) )
- continue;
-
- // Check whether the file has on of the extensions allowed for this XML sitemap
- $fileinfo = pathinfo( SITEMAP_DIR.$file );
- if ( in_array( $fileinfo['extension'], $filetypes ) ) {
+ <url>
+ <loc><?php echo SITEMAP_DIR_URL . $file ?></loc>
+ <lastmod><?php echo $mod; ?></lastmod>
+ <changefreq><?php echo $chfreq; ?></changefreq>
+ <priority><?php echo $prio; ?></priority>
+ </url>
+ <?php
+ }
+ } // End of the while loop
- // Create a W3C valid date for use in the XML sitemap based on the file modification time
- $mod = date( 'c', filemtime( SITEMAP_DIR.$file ) );
+ // Close the dir
+ closedir( $handle );
+ }
- // Replace the file with it's replacement from the settings, if needed.
- if ( in_array( $file, $replace_files ) )
- $file = $replace[$file];
-
- // Start creating the output
?>
-
- <url>
- <loc>http://<?php echo $_SERVER['SERVER_NAME'].'/'.$file ?></loc>
- <lastmod><?php echo $mod; ?></lastmod>
- <changefreq><?php echo $chfreq; ?></changefreq>
- <priority><?php echo $prio; ?></priority>
- </url><?php
- }
- } // End of the while loop
-
- // Close the dir
- closedir($handle);
- }
-
- ?>
</urlset>
Please sign in to comment.
Something went wrong with that request. Please try again.