PHP-在没有协议的情况下将页面重定向到URL


PHP - redirect page to URL without protocol?

我遇到了麻烦,不知道如何解决这个问题。

我有一个网站www.example.com。它在移动浏览器上打开,在一个操作触发重定向后,我必须重定向到something://

无论我多么努力,我都无法重定向到something://,当我重定向时:

<?php header('Location: something://'); ?>我得到的是:http://www.example.com/something://

我一直在尝试JS(location.replace.href、location.replace等),但也没有成功。

如何强制URL更改我想要的方式?

RFC 2616说:

位置=";"位置":"绝对URI

其中CCD_ 6是在RFC 2396中指定的。

  absoluteURI   = scheme ":" ( hier_part | opaque_part )
  relativeURI   = ( net_path | abs_path | rel_path ) [ "?" query ]
  hier_part     = ( net_path | abs_path ) [ "?" query ]
  opaque_part   = uric_no_slash *uric
  uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
                  "&" | "=" | "+" | "$" | ","
  net_path      = "//" authority [ abs_path ]
  abs_path      = "/"  path_segments
  authority     = server | reg_name
  reg_name      = 1*( unreserved | escaped | "$" | "," |
                      ";" | ":" | "@" | "&" | "=" | "+" )
  server        = [ [ userinfo "@" ] hostport ]
  userinfo      = *( unreserved | escaped |
                     ";" | ":" | "&" | "=" | "+" | "$" | "," )
  hostport      = host [ ":" port ]
  host          = hostname | IPv4address
  hostname      = *( domainlabel "." ) toplabel [ "." ]
  domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum
  toplabel      = alpha | alpha *( alphanum | "-" ) alphanum
  IPv4address   = 1*digit "." 1*digit "." 1*digit "." 1*digit
  port          = *digit

由此,如果使用something://协议,则需要指定authority部分-斜线不能是字符串的最后一部分,例如something://example

然而,重定向位置的最终调用始终是客户端的浏览器,出于安全原因,浏览器可能会拒绝重定向到非HTTP(s)URL。

如果您正在寻找JavaScript解决方案,请尝试以下操作:

window.location = 'customprotocol://';
window.location.assign('customprotocol://');

但如果你的应用程序(即没有任何与customprotocol://相关的内容)没有安装,很可能什么都看不到。该问题的常见解决方案是为setTimeout提供回退机制,因此,如果没有任何与customprotocol://相关的内容,只需在指定的时间后将用户重定向到任何可用的页面。

window.location = 'customprotocol://';
window.setTimeout(function() { window.location = 'http://example.com/fallback.html' }, 1);

尝试@vitozev显示的JS方法,或:

echo <<< EOF
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=something://">
EOF;

或:

header('HTTP/1.1 301 Moved Permanently');
header('Location: something://');
header ('Content-Length: 0');