Exclude direct access to files

Hello Community,

I need to exclude direct access to some file extensions (.html). I tried to use the following configuration and it doesn’t block direct access.

localhost:{$CAPTIVE_CADDY_PORT}/portal {
ext !.html
root webapps/portals/
gzip
log logs/caddyaccess.log
fastcgi / 127.0.0.1:9000 php
}

Is this achievable with caddy?

There’s two ways to achieve what you’re after.

  1. Use the internal directive to mark paths unavailable to outside requests (see docs for internal)
  2. Rewrite requests that end in .html to some forbidden path

Option 2 looks something like this:

example.com {
    status 403 /forbidden
    rewrite {
        if {path} ends_with .html
        to /forbidden
    }
}

You can stack extensions to deny like so:

rewrite {
    if {path} ends_with .html
    if {path} ends_with .php
    if_op or
    to /forbidden
}

See rewrite docs and status docs.

1 Like

This is helpful. Thanks for the infromation.

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