Feb. 12, 2021, 6:09 p.m.

How to change base url or domain in sitemap - Django

A sitemap is the first important SEO step which elaborates a website's content to search engines. It provides information to search engines about the available content on our site, which helps them to crawl webpages for indexing. Sometimes we have to change the base URL or the domain name in our sitemap. This can be because of using multiple domains to the same database or migrating the same website to another domain.

So today I am going to teach you how you can change the base URL or domain name in Django sitemap. This task is pretty much easy and simple as well.

Sitemap If you don't know how to create a sitemap in Django then you can follow the following link. There I have written the whole process of creating sitemap in simple steps. So first make sure that you know how to create a sitemap in django.

Creating sitemap in Django


Changing base url in Sitemap To change the base url first you have to go into your "sitemaps.py" file. Please note that the file name may be different obviously. The important thing is, you have to just open the python file which is responsible for the sitemap generation.

Now, to change the domain/base-url we have to override get_urls() method.
You have to do something like this :
#In sitemaps.py
from django.contrib.sitemaps import Sitemap
from Blogs.models import blogs
from django.contrib.sites.models import Site

class MyBlogSitemap(Sitemap):
   changefreq = "monthly"
   priority = 0.5
   def get_urls(self, site=None, **kwargs):
      site = Site(domain='www.NewDomain.com', name='www.NewDomain.com')
      return super(MyBlogSitemap, self).get_urls(site=site, **kwargs)
   def items(self):
      return blogs.objects.all()


Here NewDomain.com is the new domain or the base url which will be used in the sitemap instead of old domain.








Some recent posts



How to deploy a django website on pythonanywhere | django Web app deploying...

Complete process of changing database from SQLite to MySQL - How to migrat...

" How to download a file in django | Making a large file downloadable from ...

How to use proxy in python requests | http & https proxies - Scraping...

Top Django Querysets, How to filter records | Fetching data from database e...

How to change base url or domain in sitemap - Django ...

How to Make a Website - Everything you should know about web development...

What is Javascript - Features and usages ...

Top 5 Interview Questions : Tips for HR round ...

How to get job in IT : Perfect resume guide for IT job ...



View all...