【问题标题】:PHP - redirect page to URL without protocol?PHP - 将页面重定向到没有协议的 URL?
【发布时间】:2016-03-07 19:34:57
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

  • 能否显示重定向 HTTP 响应的标头?
  • 我在移动设备上做这个,所以不能真正调试它:(
  • 您总是可以使用wireshark 或类似软件来嗅探流量。
  • 它可以在桌面浏览器中使用吗?
  • 另外,请尝试使用header('Location: something://somedomain/somepath/')

标签: php url-redirection http-redirect


【解决方案1】:

如果您正在寻找 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);

【讨论】:

    【解决方案2】:

    试试@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');
    

    【讨论】:

      【解决方案3】:

      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。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-09
        • 1970-01-01
        • 2021-12-26
        • 2018-08-11
        • 1970-01-01
        • 2014-04-21
        • 2022-11-27
        • 1970-01-01
        相关资源
        最近更新 更多