首页 > 解决方案 > 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配置它的正确方法是什么,或者根本不可能这样做?

标签: nginx

解决方案


正确的方法是使用 HTTP 301 状态码:

 location /aq {
        root /var/www/frontend;
        autoindex on;

        return 301 https://example.com/#$request_uri;
    }

推荐阅读