在Firebase Hosting SPA + 2个子文件夹firebase.json上配置重定向

时间:2018-09-12 14:48:07

标签: firebase firebase-hosting

我有一个公用文件夹,如:

/public
   index.html

   /landing
     index.html

   /membership
     index.html

/public/index.html是SPA,因此对/ **的每个请求都应重写为/index.html

/ landing /和/ membership /是两个静态HTML着陆,因此每个请求都不应重写

Google支持人员建议的

firebase.json:

{
    "functions": {
        "source": "functions"
    },
    "hosting": {
        "public": "public",
        "redirects": [{
                "source": "/landing",
                "destination": "index.html"
            },
            {
                "source": "/membership",
                "destination": "index.html"
            }
        ],
        "rewrites": [{
                "source": "/membership/**",
                "destination": "/membership/index.html"
            }
        ],
        "ignore": [
            "firebase.json",
            "**/.*",
            "**/node_modules/**"
        ]
    }
}

所有内容都重定向到/index.html ...

甚至尝试过:

{
  "functions": {
      "source": "functions"
  },
  "hosting": {
      "public": "public",
      "redirects": [{
              "source": "/landing/**",
              "destination": "/landing/index.html"
          },
          {
              "source": "/membership/**",
              "destination": "/membership/index.html"
          }
      ],
      "rewrites": [{
              "source": "**",
              "destination": "/index.html"
          }
      ],
      "ignore": [
          "firebase.json",
          "**/.*",
          "**/node_modules/**"
      ]
  }
}

相同的结果,所有内容都进入/

1 个答案:

答案 0 :(得分:2)

您不必使用“重定向”。 Juste使用这些重写规则:

{
    "functions": {
        "source": "functions"
    },
    "hosting": {
        "public": "public",
        "ignore": [
            "firebase.json",
            "**/.*",
            "**/node_modules/**"
        ],
        "rewrites": [
            {
                "source": "!/@(landing|membership)/**",
                "destination": "/index.html"
            },
            {
                "source": "/landing/**",
                "destination": "/landing/index.html"
            },
            {
                "source": "/membership/**",
                "destination": "/membership/index.html"
            }
        ]
    }
}

第一个规则将重写除以登陆或成员身份开始的内容以外的所有内容。 第二条规则将处理着陆路径。 第三个将指定如何处理所有成员资格路径。