【问题标题】:Rewrite a URL in nginx without redirecting在不重定向的情况下在 nginx 中重写 URL
【发布时间】:2016-02-10 00:22:54
【问题描述】:

我正在尝试使用 nginx 创建一个重写,它会更改显示的 URL 而不会实际重定向到它。例如:http://example.com/item/rest-of-path 将变为 http://example.com/folder/rest-of-path。我一直在我的 nginx.conf 中使用此代码的不同变体:

location ~ /example {
   rewrite ^/example/(.*) /folder/$1 last;
}

但这似乎并不奏效。有什么想法我在这里出错了吗?我承认我对服务器端重写还是很陌生。

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    试试这个:

    location ~ /example {
       rewrite ^/example/(.*) /folder/$1 break;
    }
    

    使用break

    【讨论】:

    • 这不会“在不实际重定向的情况下更改显示的 URL。”。
    【解决方案2】:

    试试这个。没必要放在location块下,别忘了重新加载nginx。

    rewrite     ^/example/(.+)$     /folder/$1 last;
    

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 2012-04-10
      • 1970-01-01
      • 2011-09-28
      • 2012-05-22
      • 2020-09-30
      • 2018-11-11
      • 2013-08-11
      • 2017-02-16
      相关资源
      最近更新 更多