14
08 / 2009
I’ve been working on a new application and I wanted to do URL re-writing in LightTPD. I was having trouble coming up with a regular expression (regex) that worked. If found some references that were pretty helpful and that allowed me to accomplish what I wanted. I’m listing them here in the hopes they might be helpful to you.
- First I visited a reference that I have used before. http://www.zytrax.com/tech/web/regex.htm is a good resource to explain some of the basic features of regex. The site has a test control, but it was not as robust as I would have hoped.
- Next I found a video that covered regex for dummies at http://blog.themeforest.net/screencasts/regular-expressions-for-dummies/. The video was a good introduction, but it also referenced a great site for working on your regex.
- I then jumped to the site referenced in the video. http://gskinner.com/RegExr/ is an excellent site for experimenting with regex. It was here that I was able to come up with something that worked like I needed.
- Although my solution worked, it broke all the js and css include files that were using relative paths. I ultimately provided fully qualified URL’s and the site was up an running again with the URL re-writing. I found several sites that explained what was going on with LightTPD. Guy Rutenberg’s Blog is one that offered a solution, although the solution did not work for me. It was Lullabot.com that lead me to the idea of dynamically capturing the sites root domain to build fully realized URL’s even if the site moved.
Those were the references that worked for me. The actual mod_rewrite rule is provided below in case it’s useful as a reference.
1
2
3
4
5
| $HTTP["host"] =~ "localhost:81" {
url.rewrite-once = (
"/go/(list|view)/([0-9]+)/*$" => "/go/index.php?$1=$2"
)
} |
I hope this was helpful to you.
Update: I found another regular expression test tool that worked with the preg_match method of PHP. The Regular Expression Test Tool was helpful in finding a syntax problem for PHP that was not apparent in the gskinner.com tool. They are both excellent and the gskinner.com tool is faster to use, but this one was great for solving my PHP problem.
No comments found. Please enter a comment if you have a question or contribution.