nignx将%23转换为#

####问题简述:
项目前端使用VUE框架,用#做前端路由。在和微信公众平台交互的时候,客户扫描二维码,网页会自动跳转到项目的特定页面。
跳转使用的URL如下:

https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=wx77c6c812c7&pre_auth_code=preauthcode@@@nE8hWd2idf4185&redirect_uri=http://www.test.com/%23/weChat

因为要跳转到特定的页面,所以必须在nginx里面把http://www.test.com/%23/weChat 请求重写为http://www.test.com/#/weChat

解决方案:

nginx中的配置如下:

location /# {
    rewrite ^(.*)\#(.*)$ http://$host/#$2 redirect;
}

因为%23到了nginx里面会被识别为#,为了在http中传输特殊字符需要转义,%后面是16进制编码,%23刚好是#的ASCII值35。

参考资料:
Vue-router 中hash模式和history模式的区别
nginx兼容%23

Logo

前往低代码交流专区

更多推荐