Caddy: [Feature] http.rewrite - FallbackResource

Created on 26 Dec 2017  路  4Comments  路  Source: caddyserver/caddy

I think Apache's mod_dir FallbackResource feature would be a nice addition to Caddy's http.rewrite directive.

Ref: https://httpd.apache.org/docs/trunk/en/mod/mod_dir.html#fallbackresource
Ref: https://caddyserver.com/docs/rewrite

Something like:

rewrite [basepath] {
fallbackresource
to destinations
}

It was first requested here in the forum: https://caddy.community/t/what-is-the-equivalent-of-apaches-fallbackresource/1309

Most helpful comment

You can already specify multiple values to to. It will try in order and use the first one for which the file exists, or the last one. This is what I do for React applications:

  rewrite {
    to {path} {path}/ /
  }

See https://caddyserver.com/docs/rewrite

All 4 comments

I'm testing an Ember.js application on Caddy.

Ref: https://emberjs.com/
Ref: https://guides.emberjs.com/v2.17.0/tutorial/deploying/#toc_apache
Ref: https://guides.emberjs.com/v2.17.0/getting-started/quick-start/#toc_building-for-production
(*) Ember.js is a javascript Single Page Application framework

These Apache rewrites are suggested:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [L]
</IfModule>

Or:
FallbackResource index.html

I'm currently using this block in Caddy, but it's not perfect and it'll need adjustments from time to time:

rewrite {
    ext !.css !.js !.woff !.ttf !.gif !.jpg !.mp4
    to /
}

Is there a better option at the moment?

Could this feature be considered to better support such use-cases?

You can already specify multiple values to to. It will try in order and use the first one for which the file exists, or the last one. This is what I do for React applications:

  rewrite {
    to {path} {path}/ /
  }

See https://caddyserver.com/docs/rewrite

If I get you right, you want to rewrite everything that is not a static file to the index page.

rewrite {
  to {path} /index.html
}

And like @lbguilherme pointed out, you can have multiple values under to.

I'll be closing this until we can ascertain your request is indeed not possible yet.

@lbguilherme thanks a lot. It works perfectly with my app. The {path} {path}/ is really powerful and I didn't know it could be used that way.

@abiosoft thanks for this example. this one doesn't retain location. It might be useful in the member's area or to ensure a proper workflow if needed. I'll possibly use it in that project.

I would suggest adding @lbguilherme example to the http.rewrite Examples section.

Was this page helpful?
0 / 5 - 0 ratings