[Solved] url-rewrite fun

Added by thatseattleguy over 2 years ago

Trying something new: need to allow the client (determined by IP address) to only access certain files it is "allowed" to. This takes the form of a simple rule. In pseudocode:

if {requesting_ip_address} =~ and requested URL matches "R??.html", serve file "Rxy.html*" regardless of requested URL

Example: if client IP is, he can attempt to request file /R12.html, or /R33.html, or /RQz.html....but in all those cases he'll instead get served file "/R99.html" instead - the document he's "supposed" to see (as his IP address ends in "99").

Note that other files - not ones of the form "R??.html" - are not affected by this rule and any client can access them and the above rule isn't pertinent.

Here is what I'm trying to do to make this thing happen:

$HTTP["remoteip"] =~ "^192\.168\.1\.1(..)$" {
$HTTP["url"] =~ "R..\.html" {
url.rewrite-once = ("^.*$" => "/R%1.html")

Unfortunately, the backreference %1 seems not to be working - it's just coming out null and lighttpd requests "R.html" instead.

Do I have scoping problems such that the %1 backref isn't valid, since it's from a different conditional? If so is there any way to make this work?

Thanks for any help whatsoever on this - I'm stumped. (BTW, using lighttpd/1.4.54 under RHEL 7.6 - sorry!)

Replies (4)

RE: url-rewrite fun - Added by thatseattleguy over 2 years ago

Update: This simpler version seems to actually work (though I can't test fully as I'm remotely and can't set myself to different IPs):

$HTTP["remoteip"] =~ "^192\.168\.1\.1(..)$" {
   url.rewrite-once = (
      "^/R..\.html$" => "/R%1.html" 

...but clearly I have no real clue what I'm doing here. :) Any suggestions welcomed.

RE: url-rewrite fun - Added by gstrauss over 2 years ago

The %1 comes from the condition directly enclosing the url.rewrite* rule which is using %1, whether or not that condition has capturing parens.

Try this (untested), which tries to match 10-199 in the IP: (Your original only matched 100-199. Neither matches 1-254)

$HTTP["remoteip"] =~ "^192\.168\.1\.1(\d+)$" {
    url.rewrite-once = ("^/R..\.html$" => "/R%1.html")

RE: url-rewrite fun - Added by thatseattleguy over 2 years ago

GS - thanks much. our solutions are nearly identical so I really appreciate the confirmation that I was on the right track.

And to clarify, I was actually needing and wanting only to match IPs in the 100-199 range - no more, no less. The "restricted" devices all have IPs of that form; other devices w/IPs outside that specific range have to be unrestricted. It's not very secure, but it's what the client wants. :)

thanks again, much.

RE: [Solved] url-rewrite fun - Added by gstrauss 3 months ago

To match .100-.199

$HTTP["remoteip"] =~ "^192\.168\.1\.1(\d{2})$" {
    url.rewrite-once = ("^/R..\.html$" => "/R%1.html")

FYI: lighttpd supports CIDR masks, so to match .128-.195: $HTTP["remoteip"] == ""