Roy Tang
Roy Tang

Reputation: 5771

Nginx: Rewrite URL to hash

I have an old site which has been upgraded to an SPA frontend, so now the URLs to access certain records use the hash, so the URLs changed from like example.com/aq/12345 => example.com/#/aq/12345

This is already working fine, but I also wanted to set up a redirect so that anyone who had the old URLs would be redirected to the new URLs. I tried setting up the rewrite like this:

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

        rewrite ^/aq/(.*)/$ /#/aq/$1/ last;
    }

... but when I did this, nginx simply started looking for a file named /var/www/frontend/#/aq/[whatever]/index.html, instead of simply redirecting to /var/www/frontend/index.html with the hash in the URL. What is the proper way to configure this, or is not possible to do this at all?

Upvotes: 2

Views: 1908

Answers (1)

IVO GELOV
IVO GELOV

Reputation: 14269

The proper way is to use the HTTP 301 status code:

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

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

Upvotes: 1

Related Questions