Skip to content

Sitemap Filter Settings#

Example#

When you want full control over which URLs are submitted to IndexNow, the SitemapFilter can help. Let's imagine a basic sitemap with three URLs, each with different properties:

sitemap.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://example.com</loc>
        <lastmod>2025-01-01</lastmod>
        <changefreq>always</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc>https://example.com/page1</loc>
        <lastmod>2025-02-01</lastmod>
        <changefreq>hourly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc>https://example.com/page2</loc>
        <lastmod>2025-03-01</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
</urlset>

The date range filter options can ensure that only URLs that has been modified in 2025 are submitted to IndexNow, and furthermore we can exclude any URLs that contain the word page. This will effectively submit the URL https://example.com to IndexNow because it is the only one that meets these criteria:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from index_now import DateRange, SitemapFilter, submit_sitemap_to_index_now, IndexNowAuthentication

year_2025 = DateRange(
    start=datetime(2025, 1, 1),
    end=datetime(2025, 12, 31),
)

filter = SitemapFilter(date_range=year_2025, excludes="page")

authentication = IndexNowAuthentication(
    host="example.com",
    api_key="a1b2c3d4",
    api_key_location="https://example.com/a1b2c3d4.txt",
)

submit_sitemap_to_index_now(
    authentication, "https://example.com/sitemap.xml", filter)

Documentation#

SitemapFilter#

Configuration class for filtering sitemap URLs based on text, change frequency, date ranges and other criteria.

Attributes:

Name Type Description
change_frequency ChangeFrequency | str | None

Optional filter for URLs based on change frequency, e.g. daily, weekly, monthly, etc. Note that if no <changefreq> element is found in the sitemap entry, the filter is bypassed. Ignored by default or if set to None.

date_range DateRange | None

Optional filter for URLs based on a date range, e.g. Today, Day, DaysAgo, LaterThan, EarlierThan, etc. Note that if no <lastmod> element is found in the sitemap entry, the filter is bypassed. Ignored by default or if set to None.

contains str | None

Optional filter for URLs. Can be simple string (e.g. "section1") or regular expression (e.g. r"(section1)|(section2)"). Ignored by default or if set to None.

excludes str | None

Optional filter for URLs. Can be simple string (e.g. "not-include-this") or regular expression (e.g. r"(not-include-this)|(not-include-that)"). Ignored by default or if set to None.

skip int | None

Optional number of URLs to be skipped. Ignored by default or if set to None.

take int | None

Optional limit of URLs to be taken. Ignored by default or if set to None.

Example

Get all URLs containing section1:

Python
1
2
3
from index_now import SitemapFilter

filter = SitemapFilter(contains="section1")

Get all URLs that contain either section1 or section2:

Python
1
2
3
from index_now import SitemapFilter

filter = SitemapFilter(contains=r"(section1)|(section2)")

Exclude any URL that contains section3:

Python
1
2
3
from index_now import SitemapFilter

filter = SitemapFilter(excludes="section3")

Only the URLs modified within the past 2 days:

Python
1
2
3
from index_now import SitemapFilter, DaysAgo

filter = SitemapFilter(date_range=DaysAgo(2))

Get all URLs from January, 2025:

Python
1
2
3
4
5
6
7
8
9
from datetime import datetime
from index_now import SitemapFilter, DateRange

january_2025 = DateRange(
    start=datetime(2025, 1, 1),
    end=datetime(2025, 1, 31),
)

filter = SitemapFilter(date_range=january_2025)

Get all URLs with a change frequency set to daily:

Python
1
2
3
from index_now import SitemapFilter, ChangeFrequency

filter = SitemapFilter(change_frequency=ChangeFrequency.DAILY)

From a large sitemap, skip the first 10 URLs and take the next 20 URLs:

Python
1
2
3
from index_now import SitemapFilter

filter = SitemapFilter(skip=10, take=20)