< Instant Karma Chameleon
HTML As She Is Spoke >

[Comments] (2) Rewrite Rules: It seems I am, relatively speaking, the master of Apache rewrite rules. If you're setting up NewsBruiser and you want the URLs to look a certain way, send me email letting me know what you want them to look like and I'll come up with some rewrite rules for you to try.

Note that rewriting goes both ways. If you use rewrite rules you also need to create a Python class that rewrites the URLs NewsBruiser generates, so that it outputs the nice URLs that will trigger your rewrite rules. I can help you with this too, if you want.

Filed under:


Posted by Jarno Virtanen at Fri May 21 2004 08:07

I only want to reduce some of the cruft from the address. My NewsBruiser is installed at something like /cgi-bin/weblog/nb.cgi/ and I want to address specific weblogs with something like my.site/portal/python etc. As of now, I just have a silly redirect, see http://weblog.hotales.org/portal/python, as (my) static rewrite rules mess up some links on the web interface. This is basically just because I'm just so damned lazy. :-)

Posted by Leonard at Fri May 21 2004 09:29

As I will reveal in my forthcoming tell-all document, a NewsBruiser rewrite scheme has two parts: some rewrite rules in Apache configuration, to turn fancy URLs into NewsBruiser URLs, and some Python code in a URLRewriter class, to make the exact same changes in reverse. You probably had a rewrite rule like this:

(.*) /cgi-bin/weblog/nb.cgi/$1 [L]

The part you're missing is a URLRewriter that implements a method like this:

L = len('/cgi-bin/weblog/nb.cgi')
def rewrite(self, url):
return url[self.L:]

Now whenever NewsBruiser is tempted to output "/cgi-bin/weblog/nb.cgi/foo/bar?baz" it will output "/foo/bar?baz" instead. When they click on that link, your rewrite rule will turn it back into "/cgi-bin/weblog/nb.cgi/foo/bar?baz", and everyone will be happy.

The only problem with that is that if you changed the location of your NewsBruiser install, you'd have to change the string whose length you calculate, or you'd get weird URLs. I figure it's worth it for the speed (this code gets called a lot), but you could have something that looks cuts up to and including the first occurance of 'nb.cgi'. I may make this available as a default, since it's much easier to understand than my current default URLRewriter.

The URLWriters live in nb/plugin/system/Rewrite, and you tell NewsBruiser which one to use by putting a registration in your cfg.py file (you probably didn't need a cfg.py file until now). cfg.py.sample has a stanza that instantiates a URLRewriter. You basically assign it to self.URLRewriter ('self' in the context of cfg.py is the NBConfig object).


Unless otherwise noted, all content licensed by Leonard Richardson
under a Creative Commons License.