How to block file and directory access in Caddy v2

How to block certain file type and directory access in Caddy v2?

In caddy v1 we had

rewrite {
  if_op or
  if {file} ends_with .txt
  if {file} ends_with .md
  if {file} ends_with .mdown
  if {uri} starts_with /dir1/
  if {uri} starts_with /dir2/
  to /index.php
 }

Any pointers?

It’s much simpler in v2:

@blocked {
    path *.txt *.md *.mdown /dir1/* /dir2/*
}
respond @blocked 403

You can choose whether to rewrite or redir or respond to block those requests, it depends on your usecase which fits best for you. I just used respond above as an example.

3 Likes

Many thanks Francis, indeed much simpler in v2. As you can tell I’m upgrading some old Kirby 2 CMS sites for Caddy v2, I’ll document it on this community when I’ve done and people can then check to make sure I’ve done it in the best way.

Your time in helping is really appeciated!
Only the ‘Problem with old Kirby 2 Panel rewirstes in Caddy v2’ to fix…

1 Like

The summary of this work is now in the Wiki - See Caddy v2 file for Kirby 2 & Kirby 3 on local sites for more information.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.