nginx - Nginx:将 URL 重写为哈希
问题描述
我有一个已升级到 SPA 前端的旧站点,因此现在访问某些记录的 URL 使用哈希,因此 URL 从 example.com/aq/12345 => example.com/#/aq/12345 更改
这已经很好了,但我还想设置一个重定向,以便任何拥有旧 URL 的人都将被重定向到新 URL。我尝试像这样设置重写:
location / {
root /var/www/frontend;
autoindex on;
rewrite ^/aq/(.*)/$ /#/aq/$1/ last;
}
...但是当我这样做时,nginx 只是开始寻找一个名为 的文件,而不是简单地使用 URL 中的哈希/var/www/frontend/#/aq/[whatever]/index.html
重定向到。/var/www/frontend/index.html
配置它的正确方法是什么,或者根本不可能这样做?
解决方案
正确的方法是使用 HTTP 301 状态码:
location /aq {
root /var/www/frontend;
autoindex on;
return 301 https://example.com/#$request_uri;
}
推荐阅读
- sql - 通过选择从表中获取一些值
- python - 实现二元交叉熵损失给出了与 Tensorflow 不同的答案
- function - 致命错误:未捕获的错误:调用未定义的函数 ImageCreate()
- git - git push 错误“错误:提交消息必须包含工作项 ID”
- powershell - 用powershell区分大写字符
- javascript - 使用 setInterval 自动更改文本颜色
- javascript - 如何使用 Javascript 中的线性渐变更改 div 背景图像
- python - 与 Python 中的构建技能集相关的查询
- reporting-services - 报告字段以在 ssrs 中显示正确的错误消息
- python - 使用 Pyodbc 在远程 SQL Server 中获取数据时出错