Wordpressor
Wordpressor

Reputation: 7543

PHP - redirect page to URL without protocol?

I'm in trouble and not sure how to resolve this problem.

I have a website www.example.com. It's being opened on mobile browsers and I have to do a redirect to something:// after one action triggers redirect.

No matter how hard I try I can never redirect to something://, when I do:

<?php header('Location: something://'); ?> what I'm getting is: http://www.example.com/something://.

I've been trying with JS (location.replace.href, location.replace etc.) with no luck as well.

How do I force URL to change the way I EXACTLY want?

Upvotes: 4

Views: 2157

Answers (3)

Piskvor left the building
Piskvor left the building

Reputation: 92772

RFC 2616 says:

Location = "Location" ":" absoluteURI

where absoluteURI is specified in 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

From this, if you are using a something:// protocol, you need to specify the authority part - the slashes cannot be the last part of the string, e.g. something://example

However, the final call on where to redirect is always with the client's browser, which may refuse to redirect to non-HTTP(S) URLs for security reasons.

Upvotes: 3

Kristian Vitozev
Kristian Vitozev

Reputation: 5971

If you're looking for JavaScript solution, try the following:

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

But if your app (i.e. there's nothing associated with customprotocol://) isn't installed, is most likely to see nothing. The common solution to that problem is to provide fallback mechanism with setTimeout, so if there's nothing associated with customprotocol://, just redirect user to any available page after specified amount of time.

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

Upvotes: 1

Lavi Avigdor
Lavi Avigdor

Reputation: 4182

Try the JS method shown by @vitozev, or:

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

or:

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

Upvotes: 0

Related Questions