Nginx:在网址重写中转义# [英] Nginx: Escaping # in url rewrite

查看:2833
本文介绍了Nginx:在网址重写中转义#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC JavaScript应用程序,该应用程序需要支持Facebook共享,这意味着它需要支持唯一的OG元HTML标记.

I have a MVC JavaScript application that needs to support Facebook sharing, which means it needs to support unique OG meta HTML tags.

我正在执行Nginx重写,它将检测Facebook搜寻器是否使用该部分的适当OG标签来为应用程序的自定义版本提供服务器服务,但是Apache会忽略#号后的所有内容(因为服务器端应该这样做,因为一种浏览器功能.)我想在重写中转义#",但不确定如何在Nginx中执行该操作:

I'm doing an Nginx rewrite that will detect the Facebook crawler to server a custom version of the app with the proper OG tag for that section but Apache is ignore everything after the # sign (as server-side should do since that's a browser feature.) I would like to escape the "#" in my rewrite but am not sure how to do it in Nginx:

location / {
  if ($http_user_agent ~* 'facebookexternalhit') {
    rewrite ^(.*)$ /og.php?url=http://$host$uri;
    proxy_pass http://127.0.0.1:8080;
    break;
  }
  root /var/www/html/site.net;
}

感谢您的光临!

推荐答案

您可以,也可以不必.如果您的浏览器中有一个URL,例如http://www.example.tld/site.html#anchor,则浏览器的请求将仅包含非锚定部分:http://www.example.tld/site.html.接收到内容后,浏览器将查找名为anchor的命名锚,并滚动页面以使其内容可见.

You cannot or don't have to. If you have an URL in your browser like http://www.example.tld/site.html#anchor then your browser's request will only consist of the non-anchor part: http://www.example.tld/site.html. After receiving the content the browser will look for a named anchor called anchor and scroll the page so that its content is visible.

意味着nginx永远不会看到字符#.

Meaning nginx will never see the character #.

另一方面,如果网站包含链接,而#是URL路径部分的一部分(这是相当罕见的),则必须使用通常的URL转义%xx对其进行转义.其中xx是该字符的十六进制数-在#情况下为%23.

If, on the other hand, a website contains a link with # being part of the path part of the URL (and this is rather rare) then it has to be escaped with the usual URL escaping of %xx with xx being the hexadecimal number of that chacter -- %23 in the case of #.

这篇关于Nginx:在网址重写中转义#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆