""" Request rewrite plugin. Similar to Apache mod_rewrite. Sends a '301 Moved Permanently' status code allong with the new location of the resource. Generates a simple html page with a meta-refresh and a link to the new resource as a fallback version for clients that do not understand http header redirects. If you make any changes to this plugin, please send a patch to so I can incorporate them. Thanks! To install: 1) Put rewrite.py in your plugin directory. 2) In config.py add 'rewrite' to py['load_plugins'] 3) Add the following variables to config.py: py['rewrite_rules'] = {'^/old$': r'/new', '^/nothere/(.*)': r'/buthere/\1'} # optional, no default or: py['rewrite_rules'] = {'^/nothere/(.*)': r'http://www.example.com/here/it/is/\1'} # optional, no default Note: the key may be a string, but the value _must_ be a raw string. Dependecies: - My compatibility plugin if you're not using pyblosxom 1.2+. $Id: rewrite.py,v 1.3 2005/03/01 00:50:04 sar Exp $ """ __author__ = "Steven Armstrong " __version__ = "$Revision: 1.3 $ $Date: 2005/03/01 00:50:04 $" __url__ = "http://www.c-area.ch/code/" __description__ = "Request rewriting, similar to Apache mod_rewrite" __license__ = "GPL 2+" import re _page_template = """ 301 Moved Permanently to %(new_path)s

The requested resource %(path)s has moved to %(new_path)s.

""" def verify_installation(request): config = request.getConfiguration() retval = 1 if not 'rewrite_rules' in config: print "Missing optional property: 'rewrite_rules'" print "This would be a dict like {'^/oldpath/(.*)': r'/newpath/\1'}" return retval def _apply_rewrite_rules(rewrite_rules, path): new_path = None for rule in rewrite_rules: regexp = re.compile(rule) if regexp.search(path): new_path = regexp.sub(rewrite_rules[rule], path) return new_path #****************************** # Callbacks #****************************** def cb_handle(args): request = args['request'] http = request.getHttp() data = request.getData() config = request.getConfiguration() rewrite_rules = config.get('rewrite_rules', {}) new_path = _apply_rewrite_rules(rewrite_rules, http['PATH_INFO']) if new_path: if not new_path.startswith("http"): new_path = config['base_url'] + new_path response = request.getResponse() response.addHeader('Status', '301 Moved Permanently') response.addHeader('Content-Type', 'text/html') response.addHeader('Location', new_path) content = _page_template % {'path': config['base_url'] + http['PATH_INFO'], 'new_path': new_path } response.addHeader('Content-length', str(len(content))) response.write(content) # return True to tell pyblosxom that the request has been taken care of return 1