Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Google Sitemap

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 2016 Views
  • jam13
    Avatar
    121 Posts

    Google Sitemap Link to this post

    I posted this in the Contributions forum, but I just realised that it's obsolete so I'm reposting it here as it seems the most appropriate place.

    Here's our code for generating a Google Sitemap (couldn't find anything about this in the archives):

    code/SiteMap.php:

    <?
    class SiteMap extends SiteTree {
    static $db = array(
    );
    static $has_one = array(
    );
    function SiteTreeList() {
    $pages = DataObject::get("SiteTree", "ShowInSearch = 1");
    return $pages;
    }
    }
    class SiteMap_Controller extends ContentController {
    }
    ?>

    templates/SiteMap.ss:

    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <% control SiteTreeList %>
    <url>
    <loc>$AbsoluteLink</loc>
    </url>
    <% end_control %>
    </urlset>

    Rebuild your DB, and then add a new SiteMap page in the site root with a URL of "sitemap" (title and content are not used). Remember to hide it in menus and search on the behaviour tab.

    Your sitemap should now be visible as "/sitemap/", but Google doesn't like this as it's a directory so I add a rule to my .htaccess:

    ...
    # Google SiteMap
    RewriteRule sitemap.xml sitemap/ [NC,PT]

    RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)$
    ...

    to make it available as "sitemap.xml"

    Finally to finish off, you can add a Sitemap line to your robots.txt file to allow search engines to discover your sitemap automatically:

    User-agent: *
    Disallow:
    Sitemap: http://www.examplesite.com/sitemap.xml

  • Sigurd
    Avatar
    Forum Moderator
    628 Posts

    Re: Google Sitemap Link to this post

    Thanks for this, although we'd prefer you add it to trac, so they're more itemised.

    Unfortunately SilverStripe 2.2 will have support for sitemaps already thanks to Will Scott's work a few months ago. His work provides some additional features, but its awesome to see how short the code is for the XML sitemap to be produced by you.

    PS, love your showcase sites; you might find them being showcased more

  • jam13
    Avatar
    121 Posts

    Re: Google Sitemap Link to this post


    Thanks for this, although we'd prefer you add it to trac, so they're more itemised.

    You mean the bug tracker?


    Unfortunately SilverStripe 2.2 will have support for sitemaps already thanks to Will Scott's work a few months ago. His work provides some additional features, but its awesome to see how short the code is for the XML sitemap to be produced by you.

    Never mind - only took me a couple of hours to work out

    I wonder if there are any other things in the 2.2 branch that I'm thinking of implementing myself... Is there a definitive changelog yet? Fixing the 404 problem is pretty high up on my list at the moment.


    PS, love your showcase sites; you might find them being showcased more

    Thanks, I shall pass your compliments on to the designers. I noticed you already put the British Potato Council site in your mailshot!

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Google Sitemap Link to this post

    Is there any documentation for the Google Sitemap extension?

    I already had a static Sitemap that I left up. Did that interfere with the automatically generated Silverstripe Search Engine Sitemap? When a Search Engine bot comes along, does it receive the Silverstripe version or the static version?

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Google Sitemap Link to this post

    It would view the static file, since it's the real thing. Just type in the address to it yourself and double check.

  • Matt Hardwick
    Avatar
    Community Member
    61 Posts

    Re: Google Sitemap Link to this post

    is there anyway to add to the generated sitemap?

    2016 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.