Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

See changelog in readme

  • Loading branch information...
commit 29aa04ef5490bd71469154a5d9d5333c3d1dc4f2 1 parent ce157d1
@jdevalk authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+config.php
+.idea/
View
7 README.markdown
@@ -4,8 +4,11 @@ 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.
+Open the config.php file and configure the settings. Then check the output and if it's ok, add the script URL to Google Webmaster Tools.
## Changelog
-2012-09-29: move configuration to config.php, fix outputted URLs
+* 2012-09-29:
+ * Move configuration to config.php.
+ * Fix URL output.
+ * Add option to work recursively.
View
8 config-sample.php
@@ -3,12 +3,18 @@
* 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!
+/*
+ * The directory to check.
+ * Make sure the DIR ends ups in the Sitemap Dir URL below, otherwise the links to files will be broken!
+ */
define( 'SITEMAP_DIR', './' );
// With trailing slash!
define( 'SITEMAP_DIR_URL', 'http://www.example.com/' );
+// Whether or not the script should check recursively.
+define( 'RECURSIVE', true );
+
// The file types, you can just add them on, so 'pdf', 'php' would work
$filetypes = array( 'php', 'html', 'pdf' );
View
86 xml-sitemap.php
@@ -16,46 +16,54 @@
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
+$ignore = array_merge( $ignore, array( '.', '..' ) );
+
if ( isset( $xsl ) && !empty( $xsl ) )
- echo '<?xml-stylesheet type="text/xsl" href="'. SITEMAP_DIR_URL . $xsl . '"?>' . "\n";
-
-?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
- <?php
-
- // 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 ) ) {
-
- // Create a W3C valid date for use in the XML sitemap based on the file modification time
- $mod = date( 'c', filemtime( SITEMAP_DIR . $file ) );
-
- // 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><?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
-
- // Close the dir
- closedir( $handle );
- }
+ echo '<?xml-stylesheet type="text/xsl" href="' . SITEMAP_DIR_URL . $xsl . '"?>' . "\n";
+
+function parse_dir( $dir, $url ) {
+ global $ignore, $filetypes, $replace, $chfreq, $prio;
+
+ $handle = opendir( $dir );
+ while ( false !== ( $file = readdir( $handle ) ) ) {
+
+ // Check if this file needs to be ignored, if so, skip it.
+ if ( in_array( $file, $ignore ) )
+ continue;
+
+ if ( is_dir( $file ) ) {
+ if ( defined( 'RECURSIVE' ) && RECURSIVE )
+ parse_dir( $file, $url . $file . '/' );
+ }
+
+ // Check whether the file has on of the extensions allowed for this XML sitemap
+ $fileinfo = pathinfo( $dir . $file );
+ if ( in_array( $fileinfo['extension'], $filetypes ) ) {
+ // Create a W3C valid date for use in the XML sitemap based on the file modification time
+ $mod = date( 'c', filemtime( $dir . $file ) );
+
+ // Replace the file with it's replacement from the settings, if needed.
+ if ( in_array( $file, $replace ) )
+ $file = $replace[$file];
+
+ // Start creating the output
?>
+
+ <url>
+ <loc><?php echo $url . $file ?></loc>
+ <lastmod><?php echo $mod; ?></lastmod>
+ <changefreq><?php echo $chfreq; ?></changefreq>
+ <priority><?php echo $prio; ?></priority>
+ </url><?php
+ }
+ }
+ closedir( $handle );
+}
+
+?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><?php
+ parse_dir( SITEMAP_DIR, SITEMAP_DIR_URL );
+?>
+
</urlset>
Please sign in to comment.
Something went wrong with that request. Please try again.