pythondjangopython-3.xdjango-sitemaps

Prepend WWW to sitemap Django


My urls are without WWW, how do I add it?

<url>
<loc>https://website.com/t/483/</loc>
<changefreq>daily</changefreq>
</url>

I use the Django sitemaps.

Since I force www on all my website

I have the following in my settings,py

PREPEND_WWW = True
SITE_ID = 1

And in my urls.py

from home.sitemaps import TitleSitemap

sitemaps = {
    'titles': TitleSitemap,
}

and I have a sitemaps.py

from django.contrib.sitemaps import Sitemap
from title.models import Title

class TitleSitemap(Sitemap):

    changefreq = 'daily'
    protocol = 'https'

    def items(self):
        return Title.objects.filter(error_code__isnull=True)

https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/


Solution

  • I've been able to solve this by just changing the Domain Name value in the Admin page. So, if you have

    Domain Name: example.com

    Replace it with:

    Domain Name: www.example.com