简体   繁体   English

是否可以在 nginx 中重写为包含片段的 URL?

[英]Is it possible to rewrite to a URL with a fragment in it in nginx?

Eg例如

rewrite ^/page /#page;重写^/page /#page;

I'm not sure if this would even be possible, due to the nature of fragments (never sent to the server etc).由于片段的性质(从未发送到服务器等),我不确定这是否可能。 But since I'm rewriting TO one, rather than FROM one, I think it should work?但是由于我正在重写 TO one,而不是 FROM one,我认为它应该有效吗?

In which case how do I encode/escape that hash sign so that it doesn't start a comment..?在这种情况下,我如何编码/转义该哈希符号,以便它不会开始评论..?

Thanks!谢谢!

As you know, the fragment is used by the browser and is not sent to the server.如您所知,片段由浏览器使用,不会发送到服务器。

But you can use nginx to rewrite a request into a new URI containing a fragment, but it only makes sense if the new URI is sent to the browser, ie by using an HTTP 3xx response.但是您可以使用nginx将请求重写为包含片段的新 URI,但只有将新 URI 发送到浏览器时才有意义,即使用 HTTP 3xx 响应。

The rewrite directive will generate an HTTP 3xx response when the redirect (302) or permanent (301) flag is provided (or the replacement string starts with a scheme - see this document for more).当提供redirect (302) 或permanent (301) 标志(或替换字符串以方案开头 - 请参阅此文档了解更多信息)时, rewrite指令将生成 HTTP 3xx 响应。

For example:例如:

rewrite ^/page /#page redirect;

Adding quotation marks, could help.添加引号,可能会有所帮助。 I tried in Docker Nginx latest image (1.17.4)我在 Docker Nginx 最新镜像 (1.17.4) 中尝试过

rewrite ^/page "/#page" redirect;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM