With caddy2 and SPA like react, how can I map all routes to index.html

1. My Caddy version (caddy version): caddy2 beta 16

2. How I run Caddy:

caddy run --config Caddyfile

a. System environment:

ubuntu 18

b. Command:

d. My complete Caddyfile or JSON config:

{
  "apps": {
    "http": {
      "servers": {
        "test": {
          "listen": [":80"],
          "routes": [
            {
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "rewrite",
                          "strip_path_prefix": "/api"
                        },
                        {
                          "handler": "reverse_proxy",
                          "transport": {
                            "protocol": "http",
                            "read_buffer_size": 4096
                          },
                          "upstreams": [
                            {
                              "dial": "localhost:8080"
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": ["/api/"]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "rewrite",
                                  "uri": "/"
                                }
                              ],
                              "match": [
                                {
                                  "method": "get",
                                  "header_regexp": {
                                    "name": "accept",
                                    "pattern": "text/html"
                                  }
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "handler": "file_server",
                                  "root": "/var/www/chain-writter"
                                }
                              ]
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

3. The problem I’m having:

when I visit http://host.com/some/route, I will got 404

5. What I already tried:

I am trying to rewrite all get request which accept contains text/html to /

how can I rewrite all routes to ‘index.html’ or just /

If using Caddyfile syntax: try_files (Caddyfile directive) — Caddy Documentation

If using JSON, use a file matcher with the try_files option, then use a rewrite handler to rewrite to /index.html

1 Like

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