Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决 res.redirect 部分失败的问题 #27

Closed
Dream4ever opened this issue Jun 2, 2018 · 0 comments
Closed

解决 res.redirect 部分失败的问题 #27

Dream4ever opened this issue Jun 2, 2018 · 0 comments
Labels
Back-end Where data really come and go Server The invisible hero

Comments

@Dream4ever
Copy link
Owner

Dream4ever commented Jun 2, 2018

需求描述

部署在域名A下的 Express.js 程序,要用 res.redirect 跳转到域名B下的一个 URL,结果发现跳转后的 URL 的域名还是A,这是什么原因?

问题研究

仔细看了一遍 Express.js 的官方文档,用法没错啊!这又是什么灵异现象?

注意了,每次遇到这种“明明什么地方都是对的,但结果就是不对”的情况,那就说明一定是别的什么地方有问题。

好,冷静下来再来分析分析,为什么 URL 里的域名应该是域名B的,但跳转过去之后却还是域名A的?代码没问题,那还有别的什么地方可能影响这个域名?

啊,想起来了,IIS 里面还配置过 URL 重写规则,会不会是这里作怪?

于是打开 IIS 中配置的 URL 重写规则,仔细看了一遍,不对啊,这个 URL 并没有在重写规则中,那为什么就是跳转不过去呢?不过既然怀疑到这里了,就试一下吧。

于是添加了一条新的 URL 重写规则,强制将这个 URL 由域名A重写到域名B下面,然后再进行测试,咦,这回虽然终于能显示URL对应的页面了,不过怎么样式都没了?又查看了一下在前端显示的URL,怎么还是域名A?

啊,明白了,原来这条规则设置的是“重写”,而不是“重定向”,所以虽然成功显示了 URL 对应的页面,但是因为设置的是“重写”,所以浏览器接收到的 URL 还是域名A,那自然就会去域名A下寻找这个页面所需的静态资源,当然也就找不到了。这个问题就很好解决了,在 IIS 中把“重写”改成“重定向”,再测试一下,终于好了!

后记:虽然这个问题暂时解决了,但还是没有找到真正的原因,为什么这个 URL 必须手动配置一条重定向规则,才能跳转到正确的域名下?This is a question.

@Dream4ever Dream4ever added Back-end Where data really come and go JS Javascript Deployment Deploy content to server labels Jun 2, 2018
@Dream4ever Dream4ever changed the title 启动带 ES6 的 Express 项目 让 Express 支持 ES6 Jun 9, 2018
@Dream4ever Dream4ever changed the title 让 Express 支持 ES6 解决 res.redirect 部分失败的问题 Sep 12, 2019
@Dream4ever Dream4ever added Server The invisible hero and removed Deployment Deploy content to server JS Javascript labels Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Back-end Where data really come and go Server The invisible hero
Projects
None yet
Development

No branches or pull requests

1 participant