Question

Having some trouble converting what appears to be a simple rewrite rule in apache .htaccess to a lighttpd rule.

The apache rule: RewriteRule (.*) index.php?baseURL=$1 [L,QSA]

Essentially, the URL in its entirety is passed as baseURL parameter, and of course any other given parameters are preserved.

One caveat is that it should only apply to a single directory, and (hopefully) not include that directory in baseURL.

Currently what I have in lighttpd is:

url.rewrite-once = (
"^\/Folder\/(.*)" => "/Folder/index.php?baseURL=$0"
)

This gets the entire url and passes it as a parameter, including \Folder\ and the parameters, so http://domain/Folder/test.php?someParam=1 makes baseURL contain /Folder/test.php?someParam=1

I can parse this in php and make it work, but the point is to have the same php code in apache and lighttpd.

Was it helpful?

Solution

You've got a couple problems. $0 is the entire match, you want $1 referencing the first submatch, the (.*). Like so:

url.rewrite-once = (
    "^/Folder/(.*)" => "/Folder/index.php?baseURL=$1"
)

This still has a problem with the query string, it'll produce two ?s. E.g.

"/Folder/foo?bar=1" => "/Folder/index.php?baseURL=foo?bar=1" 

Final solution:

url.rewrite-once = (
    # Match when there are no query variables
    "^/Folder/([^\?]*)$" => "/Folder/index.php?baseURL=$1",
    # Match query variables and append with &
    "^/Folder/([^\?]*)\?(.*)$" => "/Folder/index.php?baseURL=$1&$2",
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top