ALLOW_TEST = False DEFAULT_URL = 'http://blog.dantup.com/' from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.api import mail import webob, urlparse, logging # Old Domain: New Domain, Map urls (else redirects to root) urls = { 'www.dantup.com': ('blog.dantup.com', False), 'www.dantup.me.uk': ('blog.dantup.com', False), 'www.tuppeny.com': ('blog.dantup.com', False), 'dantup-redir.appspot.com': ('blog.dantup.com', False), 'blog.dantup.me.uk': ('blog.dantup.com', True), 'feeds.dantup.me.uk': ('feeds.dantup.com', True), 'wavenotifier.dantup.me.uk': ('wavenotifier.dantup.com', True), 'wavenotifier.tuppeny.com': ('wavenotifier.dantup.com', True), 'go.dantup.me.uk': ('go.dantup.com', True), 'go.tuppeny.com': ('go.dantup.com', True), } test_urls = [ 'http://www.dantup.me.uk', 'http://www.dantup.me.uk/', 'http://www.dantup.me.uk/blah', 'http://www.dantup.com', 'http://www.dantup.com/', 'http://www.dantup.com/blah', 'http://www.tuppeny.com', 'http://www.tuppeny.com/', 'http://www.tuppeny.com/blah', 'http://blog.dantup.me.uk', 'http://blog.dantup.me.uk/', 'http://blog.dantup.me.uk/2010/mytest', 'http://feeds.dantup.me.uk', 'http://feeds.dantup.me.uk/', 'http://feeds.dantup.me.uk/2010/mytest', 'http://wavenotifier.dantup.me.uk', 'http://wavenotifier.dantup.me.uk/', 'http://wavenotifier.dantup.me.uk/2010/mytest', 'http://wavenotifier.tuppeny.com', 'http://wavenotifier.tuppeny.com/', 'http://wavenotifier.tuppeny.com/2010/mytest', 'http://go.dantup.me.uk', 'http://go.dantup.me.uk/', 'http://go.dantup.me.uk/mytest', 'http://go.tuppeny.com', 'http://go.tuppeny.com/', 'http://go.tuppeny.com/mytest', ] class MainHandler(webapp.RequestHandler): def get(self): # If we're allowed to test (eg. local), and requested /test, then output the test if ALLOW_TEST and self.request.path == '/test': self.response.out.write('

testing

') self.response.out.write('') for test_url in test_urls: self.response.out.write('') self.response.out.write('
' + test_url + ' ' + get_redirect_url(test_url) + '
') # Otherwise, just go ahead and redirect else: # Perform redirect url = get_redirect_url(self.request.url) if url: logging.info('Redirecting ' + self.request.url + ' to ' + url); self.redirect(url, permanent=True) else: # Log that we didn't know what this was, and redirect to a good default logging.error('Unable to redirect this url: ' + self.request.url); mail.send_mail_to_admins( sender='"DanTup Redirect" ', subject='Redirect Script Error', body='Unable to redirect this url: ' + self.request.url ) # Don't do permanent (301), since we don't know what this is. # Move it into the dictionary above if needed self.redirect(DEFAULT_URL) def get_redirect_url(url): scheme, netloc, path, query, fragment = urlparse.urlsplit(url) # Discard any port number from the hostname netloc = netloc.split(':', 1)[0] # Fix empty paths to be just '/' for consistency if path == '': path = '/' # Check if we have a mapping for this domain if netloc in urls: # Grab the redirect info tuple redirect_info = urls[netloc] # Root redirects if not redirect_info[1]: return 'http://' + redirect_info[0] + '/' # Paths else: return urlparse.urlunsplit(['http', redirect_info[0], path, query, fragment]) # No mapping, so return None else: return None application = webapp.WSGIApplication([("/.*", MainHandler)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main()