php Header Location 相对路径兼容性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10541439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:25:20  来源:igfitidea点击:

Header Location relative path compatibility

phphtmlredirectheader

提问by abrahab

Is this relative locationhtml header absolutely compatible with all browsers at all platforms? Any standards ?

这个相对位置html 标头是否与所有平台的所有浏览器完全兼容?有什么标准吗?

Location: some_script.php?la=2&po=2030

I mean, will it always redirect to some_script.php at the current dir or not?

我的意思是,它是否总是重定向到当前目录中的 some_script.php ?

采纳答案by David Bélanger

The standard would be this:

标准是这样的:

header('Location: http://www.mywebsite.com/yourpage.php?id=32', TRUE, 302);

But to answer your question, yes it will redirect to the page X in the current folder if you don't put a slash at first or a complete URL.

但是要回答您的问题,是的,如果您没有首先输入斜杠或完整的 URL,它将重定向到当前文件夹中的页面 X。

Here's an idea I would suggest you do for every website you do. In your primary file (the main php file you use like config or whatever), create something like that :

这是我建议您为您所做的每个网站做的一个想法。在您的主文件(您使用的主 php 文件,如 config 或其他文件)中,创建如下内容:

define('URL', 'http://www.mywebsite.com/');

So when you create a redirection, a link or whatever, you do this :

因此,当您创建重定向、链接或其他任何内容时,您可以这样做:

header('Location: '.URL.'yourpage.php?id=32', TRUE, 302);

EDIT: November 2017. As pointed by @jordanbtucker below, the HTTP spec has been updated in June 2014 (this post is from 2012) to allow relative URIs in the Location header.

编辑:2017 年 11 月。正如下面@jordanbtucker 所指出的,HTTP 规范已于 2014 年 6 月更新(这篇文章来自 2012 年)以允许 Location 标头中的相对 URI。