0

I am using Django's sitemap framework and have a sitemap index. My urls file looks like this:

urls = [
    path('', include('movies.urls')),
    path('', include('accounts.urls')),
    ...
    path('admin/', admin.site.urls),
]

urlpatterns = i18n_patterns(*urls, prefix_default_language=True,)

sitemaps = {
    'main': MainSitemap,
    'movies': MoviesSitemap,
}

urlpatterns.extend([
    path('sitemap.xml', views.index, {'sitemaps': sitemaps}),
    path('sitemap-<section>.xml', views.sitemap, {'sitemaps': sitemaps},
     name='django.contrib.sitemaps.views.sitemap'),
])

This is implemented in accordance with the recommendations in the documentation of Django.

The problem is that I always get 404 when trying to access my sitemap index: example.com/sitemap.xml. This occurs because a redirect occurs automatically to the non-existent example.com/sitemap.xml/ URL with a trailing slash.

How can I avoid a slash being appended to the .xml sitemap file? I have tried using re_path but to no avail.

Edmond
  • 59
  • 2
  • 15

2 Answers2

0

You can prevent Django from automatically appending slash to urls by putting this line in your settings file:

APPEND_SLASH = False

But I think a better way would be to use

path('sitemap.xml/', views.index, {'sitemaps': sitemaps}),

in urlpatterns. I'm not sure if the second solution works, but it should.


Update:

As can be seen in the other answer you can use re_path with optional trailing slash:

from django.urls import re_path

re_path(r'sitemap.xml/?$', views.index, {'sitemaps': sitemaps}),
therealak12
  • 1,178
  • 2
  • 12
  • 26
  • It works but it does not suit me. I need my sitemap URL to be without a trailing slash. The first solution is not suitable either because I need a trailing slash in all other URLs. – Edmond Jul 02 '21 at 10:44
  • Ok. If you set `APPEND_SLASH` to `True`, and also append use the aforementioned line in you urlpatterns, Django will automatically redirect requests without trailing slash to the path containing the trailing slash. This way you receive your sitemap on both `/sitemap.xml` and `/sitemap.xml/`. – therealak12 Jul 02 '21 at 10:48
  • Not really. I will be redirected to /sitemap.xml/. Always. This does not suit me because the content-type of the page becomes 'text/xml' instead of 'application/xml,' which, I believe, is causing troubles with Google indexing. – Edmond Jul 02 '21 at 10:54
0

You can use re_path instead of path to use regular expression in your url pattern. Use ? sign in your url like this:

from django.urls import re_path
re_path(r'sitemap.xml/?$', views.appmain, {'sitemaps': sitemaps}),
Toni Sredanović
  • 2,280
  • 1
  • 11
  • 13
  • This automatically redirects me to the sitemap URL with a trailing slash, which is why I started this question. This does not suit me because the content-type of the page becomes 'text/xml' instead of 'application/xml,' which, I believe, is causing troubles with Google indexing. – Edmond Jul 02 '21 at 10:52
  • 2
    @Edmond I confirm this works. Redirection won't happen in this case. If you're still redirected, that's because you browser cached the redirect response. Try the URL in an incognito tab. – therealak12 Jul 02 '21 at 11:00
  • 1
    Now the worst thing. Everything worked with my code from the very beginning, no need to use anything more than in Django documentation. The problem was in my browser's cache... Thanks. – Edmond Jul 02 '21 at 11:08