如何configurationnginx重写规则来剥离单页JS应用程序的索引页面,并将非哈希URLredirect到散列URL?

我是一个编写单页JS应用程序的软件开发人员。 我绝不是系统pipe理员,请耐心等待。

我想要完成的是在用户点击我的网站时使用两种潜在常见/不需要的URL模式之一发生的redirect。

  1. mysite.com/index.html – >redirect到mysite.com
  2. mysite.com/path/they/remember – >redirect到哈希版本:mysite.com/#/path/they/remember

我尝试了各种方法,但我一直在redirect循环运行。 这是我目前的configuration:

注意:您会注意到/ api还需要通过轻量级oauth代理在内部redirect所有请求,所以无论使用哪种解决scheme都不能中断此redirect。

server { # Redirect all requests through /api/ to apiproxy.php location /api { rewrite ^/api/?(.*)$ /apiproxy.php/$1 last; } location / { index index.html } # use case #1 (combined with use case #2 optionally, a url such as mysite.com/index.html/path/they/remember) location ~ /index.html/?(.*) { return 301 http://mysite.com\#/$1; } location ~ /(.*) { return 301 http://mysite.com\#/$1; } 

这个设置给了我所有的redirect循环。 有没有人有任何build议或知道如何完成我想要做的nginx方式?

首先,增加日志级别并检查你的日志。

你最后的位置块似乎是假的。

还要return 301 http://mysite.com\#/$1; 似乎不合法; 你是否想要return 301 http://mysite.com/\#/$1/;

你需要逃避#