Saturday, February 4, 2012

Joomla .html Extensions + Sitemap.xml

The Problem

You have a Joomla site with search-engine friendly (SEF) page names enabled, and you've enabled .html extensions. So your pages look something like:
http://www.my-domain.com/home.html
To top it off, you'd like to use an extension like XMap by Guillermo Vargas to automatically generate your Sitemap. So far, so good. Here's the rub: you can't figure out how to make the link to your sitemap look like:
http://www.my-domain.com/Sitemap.xml
Out of the box, XMap's path to the sitemap looks something like:
http://www.my-domain.com/index.php?option=com_xmap&view=xml&tmpl=component&id=1

The Solution - Part 1

There are a couple of things you have to do to make this happen. First, you have to edit your .htaccess file. Find the line in your .htaccess file that looks like this:
## Begin - Joomla! core SEF Section.
and add these few lines of new code just above it so that it now looks like this:

## Begin - XMap rewrite

RewriteCond %{REQUEST_URI} ^/sitemap.xml [NC]

RewriteRule .* /index.php?option=com_xmap&view=xml&tmpl=component&id=1 [L]

## End - XMap rewrite



## Begin - Joomla! core SEF Section.

Note - depending on your version of XMap, the query string (the part that follows "index.php?") might be different. To find the right path, log into Joomla! as the Administrator and find the XMap component and find your sitemap. The "XML Sitemap" link has the right query string.

At this point, you should now be able to get to your sitemap by browsing to:
http://www.my-domain.com/Sitemap.xml.html
That's already much better, and if you don't mind the .html after the .xml, you can stop here.

The Solution = Part 2

This next step is a hack to the Joomla router. It's too bad that there isn't another way around this because if Joomla updates the router, your change will be lost. Unfortunately I haven't been able to find any other way around this.

Edit the ./includes/router.php file. Find the line that looks like this - in Joomla 1.7.5, it's on line 66 of the file:
if ($app->getCfg('sef_suffix') && !(substr($path, -9) == 'index.php' || substr($path, -1) == '/')) {

And change it to look like this:
if (!stripos($path,'sitemap.xml') && $app->getCfg('sef_suffix') && !(substr($path, -9) == 'index.php' || substr($path, -1) == '/')) {

Now you should be able to browse to this!
http://www.my-domain.com/Sitemap.xml.html
You can see an example of this on the em2 website. Notice that normal pages have the familiar .html extension, but the sitemap has the .xml extension.

0 comments:

About EM Squared

We develop custom softare in C#, Java, PHP, and Python, and we build websites using Joomla, Wordpress, and Drupal.

Find out more about EM Squared