views:

55

answers:

1

I'm trying to do url rewriting with Lighttpd. I have what I need partially working. Right now I have this: http://domain.com/name/a/123 which rewrites to http://domain.com/name/a.php?pid=123

I do this with this rewrite-once rule: "^/name/a/([^/]+)"=> "/name/a.php?pid=$1"

That php page has external resources that are not getting rewritten such as the JavaScript and CSS files. Is there a way I can also have the rewrite do the following?

http://domain.com/name/a/js/file.js => http://domain.com/name/js/file.js

A: 

A rewrite rule like the following should do the job. In perl syntax, assuming $str contains http://domain.com/name/a/js/file.js

   $str =~ s/\/a\//\//

Extrapolating from the regex syntax given for your partially working regex, in Lighthttpd this should map to

  "/a/"=>"/"

What this does is, looks for "/a/" and replaces this by "/". Applying this to your example (http://domain.com/name/a/js/file.js) gives http://domain.com/name/js/file.js

Jasmeet
I'm thinking that if I can do a regex where I match an entire string if it has a / in it that should work. So my rule would be like.. "^/name/a/(match / here)" => "/name/$1" I'm not sure how to match the whole input string though.
Ronald
Writing a regex that matches a/ is easy to write, but from your question it appears that you want to rewrite the string in 2 different ways. The regex I gave you tackles the second type of rewrite. Also, looking at your partially working regex, are you sure the regex ^/name/a/([^/]+)=>"/name/a.php?pid=$1rewrites http://domain.com/name/a/123. The regex would try to match /name/.. at the beginning of the string. I think you need to remove the caret(^).
Jasmeet
Are you looking for one regex to do both the rewrites ?. If so this is not possible. In the first instance you want to transform name/a/123 to name/a.php?pid=$1. $1 will capture the substring matched by the regex in parenthesis. The original string does not contain the text a.php?pid, hence you can never capture this out of the original string
Jasmeet
Nope I'm looking for two. I tried your suggestions and they are not working.
Ronald
Try escaping the asterix, According to this (http://redmine.lighttpd.net/wiki/1/Docs:ModRewrite), both * and + need to be escaped. Also if in your input we always have a/ i.e. a followed by a front slash you can get rid of the [^/]* completely.
Jasmeet
Updated the regex to look for /a/ and replace that with /
Jasmeet
Yes the a would always be there. If I get ride of the [^/]* then there's no regex...?
Ronald
Please try the updated regex, thanks
Jasmeet
So they are to be applied in sequence, to the same set of url strings?. In that case we should use these 2 disjoint rules"/a/([0-9]+)"=>"/a.php?pid=$1" and "/a/([a-z]+)"=>"/$1"
Jasmeet