例如,
如果基本网址是 example.com,它提供 index.html 作为响应,如果请求是 example.com/path,我必须返回 index.html 作为响应。我知道使用 Web 框架可以解决这个问题,但我想用纯 JavaScript 来实现。
编辑: 如果基本网址是 example.com,那么如果我在浏览器网址栏中输入 example.com/hello 或 example.com/world 或 example.com/anything,我必须返回相同的 index.html 页面。
最佳答案
我建议阅读有关单页应用程序(SPA)的内容,例如https://medium.com/@pshrmn/single-page-applications-and-the-server-32a23d67936
简而言之,您需要知道您的 example.com 正在运行什么类型的网络服务器。您需要配置所有流量以访问您的 example.com/index.html
如果是 Apache2,那么例如,您可以创建一个 .htaccess 文件,其中包含以下行中的内容
RewriteEngine On
# set the base URL prefix
RewriteBase /
# for requests for index.html, just respond with the file
RewriteRule ^index\.html$ - [L]
# if requested path is not a valid filename, continue rewrite
RewriteCond %{REQUEST_FILENAME} !-f
# if requested path is not a valid directory, continue rewrite
RewriteCond %{REQUEST_FILENAME} !-d
# if you have continue to here, respond with index.html
RewriteRule . /index.html [L]
或者如果是 nginx,那么您将编写类似的配置
server {
...
location / {
try_files $uri /index.html;
}
}
但是仍然有很多 Unresolved 问题,在这里给出准确的答案
关于javascript - 我正在实现一个项目,我必须接受所有 url 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51902815/