2015-12-03 46 views
4

我有麻煩,不知道如何解決這個問題。PHP - 重定向頁面到URL沒有協議?

我有一個網站www.example.com。它正在移動瀏覽器上打開,在一個動作觸發重定向後,我必須重定向到something://

不管我再怎麼努力我永遠不能重定向到something://,當我做:

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

我一直在嘗試與JS(location.replace.href,location.replace等)沒有運氣以及。

我該如何強制URL改變我完全想要的方式?

+0

您可以顯示重定向HTTP響應中的標題嗎? –

+0

我在移動設備上這樣做,所以不能真正調試它:( – Wordpressor

+0

你總是可以使用'wireshark'或類似軟件嗅探流量。 –

回答

2

RFC 2616說:

位置= 「位置」,「 :「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的客戶端瀏覽器始終在最後一次重定向的位置調用。

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); 
0

嘗試通過@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');