Code

Opened 7 years ago

Closed 3 years ago

#3048 closed enhancement (wontfix)

sitemaps and prepend_www

Reported by: simonbun Owned by: adrian
Component: Contrib apps Version:
Severity: trivial Keywords: sitemaps
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The sitemaps module reads the current_site.domain value to determine the <loc> node. This yields <loc>http://mydomain.mytld/mypath/</loc>. I use the PREPEND_WWW setting however, and google does not validate my sitemap because it expects the submitted URL's to contain the 'www' subdomain.

May i suggest the trivial enhancement to take the PREPEND_WWW setting into account and prepending the 'www' to the current_site.domain value when necessary?

I'm not fully up to speed on the django core to patch it myself.

Attachments (0)

Change History (7)

comment:1 Changed 7 years ago by Esaj

Surely you should set current_set.domain to use the 'www' prefix?

comment:2 Changed 7 years ago by Esaj

I meant current_site...

comment:3 Changed 7 years ago by simonbun

Indeed, that's my interim solution, but it breaks some functionalities of the sites contrib.

comment:4 Changed 7 years ago by adrian

  • Resolution set to invalid
  • Status changed from new to closed

Which functionalities of the sites contrib does it break? The solution proposed by Esaj should work.

comment:5 Changed 7 years ago by simonbun <simonbun@…>

Well, for one the documentation says:

send_mail('Thanks for subscribing to %s alerts' % current_site.name,
        'Thanks for your subscription. We appreciate it.\n\n-The %s team.' % current_site.name,
        'editor@%s' % current_site.domain,
        [user.email])

which would render 'editor@…'

Other than that, i'm using
if current_domain == 'foo.com'
throughout my projects.

Granted, its not hard to change that, and its just my personal problem.

Aside from this all though, a domain to me is still in 'domain.tld' form, not including the 'www' host. Unless you interpret domain as fully qualified domain.

comment:6 Changed 3 years ago by niksite

  • Resolution invalid deleted
  • Status changed from closed to reopened

I have same issue now. I cannot use www in current_site.domain (I have used this variable in many places) and google webmaster tools reject all addr in sitemap because lack of 'www'.

Why we cannot have something like:

--- django_old/contrib/sitemaps/__init__.py   2008-11-19 08:44:26.000000000 +0300
+++ django_new/contrib/sitemaps/__init__.py     2010-11-24 18:57:34.000000000 +0300
@@ -1,4 +1,5 @@
 from django.core import urlresolvers, paginator
+from django.conf import settings
 import urllib
 
 PING_URL = "http://www.google.com/webmasters/tools/ping"
@@ -29,7 +30,10 @@
 
     from django.contrib.sites.models import Site
     current_site = Site.objects.get_current()
-    url = "http://%s%s" % (current_site.domain, sitemap_url)
+    domain = current_site.domain
+    if settings.PREPEND_WWW and not domain.startswith('www.'):
+        domain = 'www.' + domain
+    url = "http://%s%s" % (domain, sitemap_url)
     params = urllib.urlencode({'sitemap':url})
     urllib.urlopen("%s?%s" % (ping_url, params))
 
@@ -62,9 +66,12 @@
     def get_urls(self, page=1):
         from django.contrib.sites.models import Site
         current_site = Site.objects.get_current()
+        domain = current_site.domain
+        if settings.PREPEND_WWW and not domain.startswith('www.'):
+            domain = 'www.' + domain
         urls = []
         for item in self.paginator.page(page).object_list:
-            loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
+            loc = "http://%s%s" % (domain, self.__get('location', item))
             url_info = {
                 'location':   loc,
                 'lastmod':    self.__get('lastmod', item, None),

and

--- django_old/contrib/sitemaps/views.py      2008-11-19 08:44:26.000000000 +0300
+++ django_new/contrib/sitemaps/views.py        2010-11-24 18:57:58.000000000 +0300
@@ -4,6 +4,8 @@
 from django.core import urlresolvers
 from django.utils.encoding import smart_str
 from django.core.paginator import EmptyPage, PageNotAnInteger
+from django.conf import settings
+
 
 def index(request, sitemaps):
     current_site = Site.objects.get_current()
@@ -15,10 +17,13 @@
         else:
             pages = site.paginator.num_pages
         sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section})
-        sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url))
+        domain = current_site.domain
+        if settings.PREPEND_WWW and not domain.startswith('www.'):
+            domain = 'www.' + domain
+        sites.append('%s://%s%s' % (protocol, domain, sitemap_url))
         if pages > 1:
             for page in range(2, pages+1):
-                sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page))
+                sites.append('%s://%s%s?p=%s' % (protocol, domain, sitemap_url, page))
     xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
     return HttpResponse(xml, mimetype='application/xml')

This fix works well for me, but it will be great to have this into official django repo.

comment:7 Changed 3 years ago by bretth

  • Resolution set to wontfix
  • Status changed from reopened to closed

This has already been closed by a core dev. Please move discussion to the developers mailing list if you wish to argue why the functionality should now be added.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.