首页 > 解决方案 > Converting .htaccess to lighttpd for a subdirectory

问题描述

I'm a newbie and am trying to convert a htaccess to lighttpd format but everything I try doesn't work. I have a few rules for example.com and now I want to add a new subdirectory named /test and use htaccess rules just for that directory (is that even possible?). Anyway, here's the htaccess I want to convert:

Options +FollowSymlinks

RewriteRule ^.*\.(gif|png|jpe?g|bmp|css|js|swf|wav|avi|mpg|ttf|woff)$ - [NC,L]

RewriteRule ^dashboard/ - [R=403,L,NC]
RewriteRule ^admin/ - [R=403,L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.sa$ index.php?sa=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [PT,L]

Now, my guesswork solution was this, but it doesn't work and I don't understand how can I make it work:

   $HTTP["url"] =~ "^/test" {
$HTTP["url"] !~ "^/(dashboard|admin)/" {
    url.access-deny = ("")
}
url.rewrite-if-not-file = (
    "^/(.*)$" => "/index.php?uri=$1",
    "^/(.*/).sa$" => "index.php?sa=$1",
)
server.follow-symlink = "enable"}

Can anyone help? Thanks

标签: .htaccessmod-rewritelighttpd

解决方案


lighttpd 重写规则与完整的 url-path + query-string 匹配,而$HTTP["url"]只是 url-path(是的,我可以看到这可能有点令人困惑)并且$HTTP["query-string"]只是 query-string。

以下使用了 lighttpd 1.4.50(一年多前发布)中可用的一些语法。 lighttpd 的当前版本是 lighttpd 1.4.54。(例如,如果您使用的是 lightttpd 1.4.45,那么您的发行版会落后一点)

$HTTP["url"] !~ "^/(dashboard|admin)/" {
    url.access-deny = ("")
}
$HTTP["url"] !~ "\.(?i:gif|png|jpe?g|bmp|css|js|swf|wav|avi|mpg|ttf|woff)$" {
    url.rewrite-if-not-file = (
        "^(/.*)\.sa(\?.*)?$" => "index.php?sa=$1${qsa}",
        "" => "/index.php?uri=${url.path}${qsa}",
    )
}
server.follow-symlink = "enable"

推荐阅读