getgrav / grav-plugin-sitemap

Grav Sitemap Plugin
https://getgrav.org
MIT License
42 stars 42 forks source link

sitemap URL and stylesheet XSL support #15

Closed danreb closed 8 years ago

danreb commented 8 years ago

Just installed the sitemap and it was accessible using URL http://example.com/sitemap and http://example.com/sitemap.xml

Is there a way to limit the URL? I mean just allow to be accessible using the second URL (sitemap.xml)

I don't want someone tried to visit the url and seen a page that seems to be forgotten to be styled.

Also I tried to create XSL stylesheet and attached it to sitemap.xml.twig that I've copied into my theme folder and it is not working unless I've stripped out the xmlns protocol in urlset tag. What is the right way of doing this?

Thanks in advance.

flaviocopes commented 8 years ago

I don't see a way for Grav to do it, as /sitemap is a page, and must have an URL. You can try adding an .htaccess rule to deny access to it, try if that works (not sure).

Not sure on the XSL actually, can you paste the whole code you used, so I can try it?

danreb commented 8 years ago

Here's the content of sitemap.xml.twig that is stripped out tag but working with XSL stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="{{ theme_url }}/sitemap.xsl"?>
<urlset>
    {% for entry in sitemap %}
    <url>
        <loc>{{ entry.location|e }}</loc>
        <lastmod>{{ entry.lastmod }}</lastmod>
        {% if entry.changefreq %}
        <changefreq>{{ entry.changefreq }}</changefreq>
        {% endif %}
        {% if entry.priority %}
        <priority>{{ entry.priority|number_format(1) }}</priority>
        {% endif %}
    </url>
    {% endfor %}
</urlset>

and here's the content of my XSL stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
  <h2>XML Sitemap</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Links</th>
      <th>Last Modified</th>
    </tr>
    <xsl:for-each select="urlset/url">
    <tr>
      <td><xsl:value-of select="loc"/></td>
      <td><xsl:value-of select="lastmod"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 
danreb commented 8 years ago

If tag is not stripped with xmlns attribute, the stylesheet is not working

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

don't know why it doesn't want xmlns attribute

Here's an example of styled one -> http://rosesandblooms.s14-host.com/sitemap.xml

and here's the unstyled one that i want to git rid of -> http://rosesandblooms.s14-host.com/sitemap

I will try the .htaccess rule if it will works

flaviocopes commented 8 years ago

This is a problem of your XSL, as you must use the namespace defined in the XML (see http://stackoverflow.com/a/3836188)

This works:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:s="http://www.sitemaps.org/schemas/sitemap/0.9"
 exclude-result-prefixes="s"
>

 <xsl:template match="/">
  <html>
    <body>
      <h2>Sitemap</h2>
      <table border="1">
        <tr bgcolor="#9acd32">
          <th>Location</th>
          <th>Last Modified</th>
          <th>Update Frequency</th>
          <th>Priority</th>
        </tr>
        <xsl:for-each select="s:urlset/s:url">
          <tr>
            <td><xsl:value-of select="s:loc"/></td>
            <td><xsl:value-of select="s:lastmod"/></td>
            <td><xsl:value-of select="s:changefreq"/></td>
            <td><xsl:value-of select="s:priority"/></td>
          </tr>
        </xsl:for-each>
      </table>
    </body>
  </html>
 </xsl:template>
</xsl:stylesheet>
flaviocopes commented 8 years ago

I added a PR to add a XSL to the plugin itself: https://github.com/getgrav/grav-plugin-sitemap/pull/16

danreb commented 8 years ago

Nice... thanks

danreb commented 8 years ago

I just want to mention that I was able to git rid of the unstyled page by using a .htaccess redirect trick

I added this to the bottom of my .htaccess

Redirect 301 /sitemap /sitemap.xml

Thanks