Rewrite with {hostonly}

Hello, I’m trying to rewrite a subdomain over to a path segment so that I can transform something like http://{branch}.{project}.example.com over to s3://bucket/{project}/{branch}. I don’t think this is possible given the current rewrite functionality but I’m hoping I’m wrong. Here’s as far as I got,

example.com, *.*.example.com
gzip
log stdout
errors stdout
proxy / http://bucket-name.s3-website-us-east-1.amazonaws.com
rewrite {
  r ([^.]+)\.([^.]+)\.example.com
  to /{2}/{1}
}

But, this doesn’t work because rewrite's regexp is on the path not the domain and if (where I can match the domain) doesn’t pass captured groups to the destinations.

As far as I am aware, you can only rewrite the URI. To achieve this functionality with Caddy may require maintaining a list of vhosts for this purpose, perhaps you could import from a directory containing files for each branch/project.

Thanks @Whitestrake. That’s what I was exploring next… importing a folder containing a bunch of branch.project.example.com.conf files that list each rewrite with a hardcoded s3 path.

However, not willing to give up… does anyone know of a way to inject request specific environment variables to a Caddy request (similar to nginx’s Lua component)? I could see doing the parsing elsewhere and just leaving Caddy with a {$REQUEST_BRANCH} or {$REQUEST_PROJECT} environment variable.

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