繁体   English   中英

PHP - 将页面重定向到没有协议的URL?

[英]PHP - redirect page to URL without protocol?

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

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

无论我怎么努力,我都无法重定向到something:// ,当我这样做时:

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

我一直在尝试使用JS(location.replace.href,location.replace等),但也没有运气。

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

RFC 2616说:

Location =“Location”“:”absoluteURI

其中absoluteURIRFC 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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM