开发者

Django Generic Sitemap and Appending slashes

开发者 https://www.devze.com 2023-03-10 12:47 出处:网络
Google is complaining of a 301 redirect f开发者_JS百科or URLs in my sitemap. I\'m using GenericSitemap, and my APPEND_SLASH setting is defaulted (on).My URLs therefore redirect so they end with a sla

Google is complaining of a 301 redirect f开发者_JS百科or URLs in my sitemap.

I'm using GenericSitemap, and my APPEND_SLASH setting is defaulted (on). My URLs therefore redirect so they end with a slash, which is how I like it. However the default django sitemap (django.contrib.sitemaps) doesn't put these trailing slashes on the urls it generates.

I think I've followed all the docs correctly and can't find any answers on google - any ideas?


According to the source code of django.contrib.sitemaps, get_absolute_url is used for building the sitemap, so the best thing would probably be starting to convert your urls model by model. However, if it causes you too much problems, you can meanwhile subclass GenericSiteMap to add slashes to all urls without a trailing slash:

class SlashedGenericSitemap(GenericSitemap):
    def location(self, obj):
        url = obj.get_absolute_url()
        return url if len(url)>0 and url[-1]=='/' else url + '/'

And of course, use it instead of GenericSiteMap.

0

精彩评论

暂无评论...
验证码 换一张
取 消