I am Hack Sparrow
Captain of the Internets.

How to Generate Compressed XML Sitemaps Using PHP

Creating Compressed XML Sitemap - sitemap.xml.gz

So you figured your 5 mb-and-growing dynamically generated XML sitemap has been a major bandwidth hog and are scared it might bring your website  on its knees; or maybe you wanna play nice with Google by serving it a fast loading compressed XML sitemap. Whatever be the reason, you are here to find out how to compress your xml sitemap and output it as a .gz file, something like sitemap.xml.gz.

Compressed XML Sitemap Using Apache

There is quite some technicality involved in the mechanism behind outputting compressed content using Apache, I'll leave that to others and get straight to the point. Do a phpinfo() and look for the strings "deflate" and "gzip", if you found them, you are ready to start outputting compressed content right away, all you have to do is send the header header('content-type: application/x-gzip') to send the content as a gzipped file. Example:

$data = create_dynamic_xml_sitemap(); // assume this function generates your dynamic XML sitemap string
header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
echo $data; // this echo will actually prompt the user to download a .gz file

That's it. On seeing the header, Apache will take the string content, gzip it on the fly and send it to the user as a gzipped file. The filename property helps to let the user know that it's a gzipped file.

Compressed XML Sitemap Using PHP

If for some reason Apache does not support gzipping - old version, modules not enabled etc, you can use PHP to generate the compressed XML sitemap. PHP comes with a module of its own to generate gzipped content. Here's how you do it:

$file_name = 'sitemap.xml';
header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="'.$file_name.'.gz"');
$data = create_dynamic_xml_sitemap(); // assume this function generates your dynamic XML sitemap string
$gzdata = gzencode($data, 9);
echo $gzdata;

The above code will generate the compressed XML sitemap on a server where Apache does not support compression. We still have to send the header('content-type: application/x-gzip') header to let the client know that we are sending a gzipped content.

Notes

  1. Don't use the second example on a server that supports gzip on its own else you will end up with a double-gzipped file.
  2. Make sure your XML sitemap is valid and well-formed before gzipping it.

Exercise
  1. Use PHP to generate a compressed file on a server which supports compression on its own, without the double-gzip problem.
  2. How will you make the browser load the gzip file in it, rather than download it?
  3. What are HTTP Headers?
  4. What is MIME?

References
  1. Gzip
  2. application/x-gzip
  3. PHP gzencode()
  4. Apache Module mod_deflate
  5. XML Sitemaps
  6. Wikipedia: Site Map

2 Responses to “How to Generate Compressed XML Sitemaps Using PHP”

  1. Mamoon says:

    Thank you, it helped me.

    Regards.

  2. Tyrone says:

    I have been using Winrar to compress an xml file into an rar file. Then I rename it sitemap.xml.gz.

    When I click on the file icon it opens in a new widows, as it should, showing the perfecty written sitemap.xml file.

    What doesn’t someome just write conversion software. The free online website no longer supports xml.gz compression.

Make a Comment