If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

ADD This Code in Your Default.py :

def sitemap():
        # Import Os and Regex
        import os
        from gluon.myregex import regex_expose

        # Finding You Controllers 
        ctldir = os.path.join(request.folder,"controllers")
        ctls=os.listdir(ctldir)
        
        # Excluding The appadmin.py and the Manage.py
        if 'appadmin.py' in ctls: ctls.remove('appadmin.py')
        if 'manage.py' in ctls: ctls.remove('manage.py')

        # Adding  Schemas for the site map
        xmlns='xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"\n' 
        xmlnsImg='xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"\n'
        xmlnsVid='xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"\n'
        sitemap_xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
        sitemap_xml +='<urlset %s  %s  %s>\n'%(xmlns,xmlnsImg,xmlnsVid)

        # Add The Pages That You Dont want in the XML Sitemap
        ExcludedPages = ['Function_Name','Function_Name']

        # Define Your Domain
        Domain = 'http://Domain.com'

        for ctl in ctls:
                if ctl.endswith(".bak") == False:
                        filename = os.path.join(ctldir,ctl)
                        data = open(filename, 'r').read()
                        functions = regex_expose.findall(data)
                        ctl = ctl[:-3].replace("_"," ")

                        # Adding Statics URLs From Your Controllers 
                        for f in functions:
                            # Ignore the Pages from the list above (  ExcludedPages )
                            if f not in ExcludedPages:
                                sitemap_xml += '<url>\n<loc>%s/%s/%s</loc>\n</url>\n' %(Domain,ctl,f.replace("_"," "))

        #  Dynamic URLs From Tables  For ex ... >> www.domain.com/post/1
        posts = db().select(db.posts.ALL, orderby=~db.posts.created)
        for item in posts:
                sitemap_xml += '<url>\n<loc>%s/default/post/%s</loc>\n</url>\n' %(Domain,item.id)
        sitemap_xml +='</urlset>'
        return sitemap_xml

 

Then Open  http://www.domain.com/sitemap.xml

Done with the help of this post https://groups.google.com/forum/?fromgroups=#!topic/web2py/TUMn6R3BJ10

 

 

Enjoy , Best Regards,

Hassan ALnatour

Related slices

Comments (1)


Hosting graciously provided by:
Python Anywhere