Redirect all subdirectory requests to single file with requested directory intact

0

I want to have a directory on my lighttpd server that will redirect all requests to a main index.php file, but with the PHP to be able to interpret the original request URI.

For example, both www.server.com/directory/foo and www.server.com/directory/bar would run www.server.com/directory/index.php, but the PHP context would be able to see the domain (with something like $_SERVER['uri']?) and use an internal PHP router to figure out what to do.

Currently I've tried a few variations on this:

$HTTP["url"] =~ "^/directory/.*" {
    url.redirect = (
        "^/.*/assets/(.*)" => "/dir/assets/$1",
        "^/.*/(.*)" => "/dir/index.php"
    )
}

but the index.php is unable to see the original route. The assets thing works though. Thanks!

cogm

Posted 2019-09-14T22:16:00.273

Reputation: 71

I do it on apache2 by redirecting to the PHP script if a file or directory matching the request isn't found. This kicks in before a 404 is sent to the client, so it is transparent on that end. – ivanivan – 2019-09-14T22:27:02.497

Answers

0

Solved it, in case anybody else needs:

$HTTP["url"] =~ "^/directory/.*" {
    url.rewrite-once = (
        "^/.*/assets/(.*)" => "/directory/assets/$1",
        "^/[^\?]*(\?.*)?" => "/directory/index.php$1"
    )
}

Forwards on any query params as well.

cogm

Posted 2019-09-14T22:16:00.273

Reputation: 71