This is what I ended up with. Slightly different approach: import urlparse
def sUrl(s): page = group = '' bits = urlparse.urlsplit(s) url = '//'.join([bits[0],bits[1]]) + '/' query = bits[2].split('/') if '' in query: query.remove('') if len(query) > 1: page = query.pop() if len(query) > 0 and query[-1] == query[-1].capitalize(): group = query.pop() if len(query): url += '/'.join(query) + '/' if page == '': page = 'Main' if group == '': group = 'Main' page = '.'.join([group,page]) print " URL: (%s) PAGE: (%s)" % (url, page) urls = [ "http://www.example.org/test/Main/AnotherPage", # (page = Main/AnotherPage) "http://www.example.org/test/Main", # (page = Main + '/' + default_page) "http://www.example.org/test", # (page = default_group + '/' + default_page) "http://www.example.org/test/", # (page = default_group + '/' + default_page) "http://www.example.org/", # (page = default_group + '/' + default_page) "http://www.example.org/Main/AnotherPage", ] for u in urls: print "Testing:",u sUrl(u) -- http://mail.python.org/mailman/listinfo/python-list