dongyilai4214 2015-09-26 03:47
浏览 44

Apache / php重定向循环

Here is the situation in the smallest explanation I can give.

From php, I do a redirect with the following function

function redirect($loc)
{
    session_write_close();
    header("Location: $loc");
    exit();
}

For example, redirect('http://www.google.com/');

The problem is that when I attempt to do redirect('https://local.application.com') the browser gets a header that says Location: https://local-apps.application.com. https://local-apps.application.com is the uri that the original request comes through.

I've used xdebug to step through the php code right up to header('Location: $loc'); call and I can verify 100% that $loc == 'https://local.application.com' because xdebug tells me so. However, for SOME reason, the browser gets Location: https://local-apps.application.com. This causes an infinite loop as https://local-apps.application.com once again redirects to https://local.application.com.

What is maybe going on?!

The extremely weird part is that putting the SAME EXACT code in our qa environment yields the expected behavior and NO INIFINITE loop is caused. In qa the urls are qa-apps.application.com and qa.application.com.

I don't know if my local Apache is doing this. I doubt it's php's fault because xdebug shows me for certain that the $loc variable is set to the right uri and I'm doing an exit right after.

I've also tried using Charles web proxy to act as a man in the middle to look at traffic but it also shows Location: https://local-apps.application.com instead of the expected Location: https://local.application.com.

Is there an Apache setting that would alter outgoing headers? What should I do?

EDIT 1

I altered my Apache access log to print out the Location header in the response and it is the correct value. So apache isn't doing anything weird here.

EDIT 2

I removed all entries from my /etc/hosts file. I then installed dnsmasq and set it up as the primary DNS server for my computer. I tested this by doing dig @127.0.0.1 local.application.com and then dig @127.0.0.1 www.google.com to make sure I had set it up correctly.

I added address=/local.application.com/127.0.0.1 and address=/local-apps.application.com/127.0.0.1 to the dnsmasq config and restarted dnsmasq. I even restarted my computer after setting it up as a daemon that starts on reboot.

I get the same weird behavior on my local machine. My webbrowser goes into an infinite loop.

More info if you are curious

I'm putting together an oauth2 implementation in my workplace.

The authorize point exists on my local machine at local-apps.application.com/authorize (local-apps) because that's where mobile apps will access it. In production it would be apps.application.com/authorize. (Sorry, I can't control the subdomains being used).

Depending on the client_id in the initial oauth2 auhorize request I will first redirect to a client appropriate domain. From local-apps.application.com/authorize I'll go to local.client-1.com/authorize and then from there I would redirect to local.client-1.com/login if the oauth2 credentials are validated successfully.. Why the extra redirect? 1. A limitation in the way our templating system works and the fact that it depends on the domain to load the proper html. 2. The sessions for php don't transfer across domains so I have to start the php session on the right domain. I can't change it. I just have to deal with it. Please don't make suggestions related to this url scheme thingy. I just want to know why php redirects to one url and the browser get a different url in the header.

  • 写回答

1条回答 默认 最新

  • duan0513 2015-09-26 04:27
    关注

    It seems, you have a same ip addresses in /etc/hosts file or you can check your Apache virtual host file to make sure the correct path. Note: Apache configuration files location and directory structure depends on the linux distro.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误