简体   繁体   English

识别到 URL 路径模式时重定向到页面

[英]Redirect to page when a URL path pattern is identified

I'd like any traffic that comes to:我想要任何流量:

mysite.com/suppliers/add-supplier

To be directed to add-supplier.html when the path is identified in my firebase.json config file.当在我的firebase.json配置文件中标识路径时,将被定向到add-supplier.html I've tried this:我试过这个:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ],
    "redirects": [
      {
        "source": "/suppliers/add-supplier",
        "destination": "/add-supplier.html"
      }
    ],
    "cleanUrls": true
  },
  "storage": {
    "rules": "storage.rules"
  }
}

Even though this identifies and redirects correctly, it gets rid of the suppliers/add-supplier path and makes it mysite.com/add-supplier即使这正确识别和重定向,它也摆脱了suppliers/add-supplier路径并使其成为mysite.com/add-supplier

Is there a way to retain the original URL while also redirecting to the correct HTML page?有没有办法在保留原始 URL 的同时重定向到正确的 HTML 页面?

You have two options:你有两个选择:

  1. Use a rewrite instead of a redirect:使用重写而不是重定向:
"rewrites": [
  {
    "source": "/suppliers/add-supplier",
    "destination": "/add-supplier.html"
  },
  {
    "source": "**",
    "destination": "/index.html"
  }
]
  1. Remove the redirect entirely, and place your file at suppliers/add-supplier.html instead of in the root完全删除重定向,并将您的文件放在suppliers/add-supplier.html而不是根目录中

I combined @samthecodingman's answer with some changes and got it working:我将@samthecodingman 的答案与一些更改结合起来并使其正常工作:

Moved page to new directory将页面移动到新目录

I moved the add-supplier.html file to a new folder called suppliers inside the public directory so that it's suppliers/add-supplier.html我将add-supplier.html suppliers移动到公共目录中一个名为 providers 的新文件夹中,因此它是suppliers/add-supplier.html

firebase.json: firebase.json:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      },
      {
        "source": "/suppliers/add-supplier",
        "destination": "suppliers/add-supplier.html"
      }
    ],
    "cleanUrls": true
  },
  "storage": {
    "rules": "storage.rules"
  }
}

This will correctly load the page but the CSS, media and JS files will not be identified.这将正确加载页面,但不会识别 CSS、媒体和 JS 文件。 To fix this, change all your resource files to have ../ in the front:要解决此问题,请将所有资源文件更改为../在前面:

before changing: <link href="dist/css/style.css" rel="stylesheet" type="text/css">更改前: <link href="dist/css/style.css" rel="stylesheet" type="text/css">

after changing: <link href="../dist/css/style.css" rel="stylesheet" type="text/css">更改后: <link href="../dist/css/style.css" rel="stylesheet" type="text/css">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM