所以呢,你要在服务端增加一个覆盖所有情况的候选资源:如果 URL 匹配不到任何静态资源,则应该返回同一个 index.html 页面,这个页面就是你 app 依赖的页面。
apche
- <IfModule mod_rewrite.c>
- RewriteEngine On
- RewriteBase /
- RewriteRule ^index\.html$ - [L]
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteRule . /index.html [L]
- </IfModule>
复制代码
nginx
- location / {
- try_files $uri $uri/ /index.html;
- }
复制代码
原生 Node.js
- const http = require('http')
- const fs = require('fs')
- const httpPort = 80
- http.createServer((req, res) => {
- fs.readFile('index.htm', 'utf-8', (err, content) => {
- if (err) {
- console.log('We cannot open "index.htm" file.')
- }
- res.writeHead(200, {
- 'Content-Type': 'text/html; charset=utf-8'
- })
- res.end(content)
- })
- }).listen(httpPort, () => {
- console.log('Server listening on: http://localhost:%s', httpPort)
- })
复制代码
基于 Node.js 的 Express对于 Node.js/Express,请考虑使用 connect-history-api-fallback 中间件。
Internet Information Services (IIS)
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <system.webServer>
- <rewrite>
- <rules>
- <rule name="Handle History Mode and custom 404/500" stopProcessing="true">
- <match url="(.*)" />
- <conditions logicalGrouping="MatchAll">
- <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
- <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
- </conditions>
- <action type="Rewrite" url="/" />
- </rule>
- </rules>
- </rewrite>
- </system.webServer>
- </configuration>
复制代码
其他web服务器配置angular伪静态参考:
https://router.vuejs.org/zh/guide/essentials/history-mode.html#%E5%90%8E%E7%AB%AF%E9%85%8D%E7%BD%AE%E4%BE%8B%E5%AD%90
|