How to change base URL or domain in sitemap - Django

A sitemap is the first important SEO step that 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 a 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 the 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 the old domain.

Recent Articles

Changing Database from SQLite to MySQL - Django

One of the main benefit to use django for web development is that it comes with an inbuilt database/filesystem i.e SQLITE. Managing database and files is super easy in django. But there are some drawbacks of using SQLITE...

  - 2023-07-12
How to download a file in django

Sometimes we have to serve a file which may be a large file from our project directory. Suppose you want to transfer a file from your project directory to your friend or to some other person/server/platform....

  - 2023-07-12
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....

  - 2023-07-12
How to Make a Website

After reading this article you will never ever search for web development guide neither on Google nor on YouTube....

  - 2023-07-12
What is Javascript - Features and usages

I'm kind of a person who always likes to get started by installing the things & writing some code. I love that but this time JavaScript requires a little bit of the theoretical basic so that we can understand what things we are learning, how hard and powerful it is and where they can be applied....

  - 2023-07-12
Top 5 Interview Questions : Tips for HR round

In this article we are talking about the most famous HR questions and the mind-set to answer those HR questions....

  - 2023-07-12
How to get job in IT - Perfect resume guide

In today's article I want to talk about how to prepare the best resume to get a job in IT. Yes, everybody wants to have a job and in order to get a job you have to apply for it and when you apply for that, the resume is one good thing that you always submit....

  - 2023-07-12


^