Lighttpd Deny Access To Folders / Directories

If users try to open a Web page (http://example.com/dir1/file.php); I’d like to display a “URL Access Denied” message for /dir1/, /www/, and /dir2/cache/ directories under lighttpd web server? How do I configure lighttpd to deny access to directory?

The mod_access module is used to deny access to files and directories. Edit /etc/lighttpd/lighttpd.conf file as follows:
# vi lighttpd.conf
Add the following code to enable mod_access:
server.modules += ( "mod_access" )
Finally add regex as follows:

ADVERTISEMENTS

# deny access to /dir1
$HTTP["url"] =~ "^/dir1/" {
     url.access-deny = ("")
}
# deny access to /dir2/cache/
$HTTP["url"] =~ "^/dir2/cache/" {
     url.access-deny = ("")
}
# add other config below

Save and close the file. Restart the lighttpd web server (first, check for syntax error):
# lighttpd -t -f /etc/lighttpd/lighttpd.conf
# service lighttpd restart

See also:

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
2 comments… add one
  • ali Feb 10, 2011 @ 14:47

    But is there a way to do it in a dynamic way? Like in .htaccess file?

  • Niko Mar 14, 2013 @ 16:26

    Is there a way to write several folders in the single line? Like “^/dir1/” + “^/dir2/cache/”

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.