25

We are all familiar with relative paths: A relative path to ./images/hello.jpg from http://www.domain.com/hey links to http://www.domain.com/hey/images/hello.jpg.

Problem: How do you state a relative path to http://www.domain.com:1234 when you are at http://www.domain.com/hey?

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • I don't think you can. The port is effectively part of the domain name in this context. By specifying the domain name without the port (not using a relative link) you are basically supplying a _different_ domain. – Michael Berkowski Nov 29 '11 at 20:12
  • 1
    See also http://stackoverflow.com/q/6016120/60075 – Craig McQueen Nov 23 '12 at 02:55

4 Answers4

23

You cannot change any part of the authority (i.e. the host:port part) in relative URLs. See the algorithm described in section 5.2.2 of RFC 3986 to see how relative URLs are interpreted. Important thing to notice is that authority is simply copied from base URL or from the URL being resolved and authority's structure is never interpreted. This implies that you cannot change any of its parts, including the port part.

Here's the algorithm in pseudo-code copied from the RFC:

  -- The URI reference is parsed into the five URI components
  --
  (R.scheme, R.authority, R.path, R.query, R.fragment) = parse(R);

  -- A non-strict parser may ignore a scheme in the reference
  -- if it is identical to the base URI's scheme.
  --
  if ((not strict) and (R.scheme == Base.scheme)) then
     undefine(R.scheme);
  endif;

  if defined(R.scheme) then
     T.scheme    = R.scheme;
     T.authority = R.authority;
     T.path      = remove_dot_segments(R.path);
     T.query     = R.query;
  else
     if defined(R.authority) then
        T.authority = R.authority;
        T.path      = remove_dot_segments(R.path);
        T.query     = R.query;
     else
        if (R.path == "") then
           T.path = Base.path;
           if defined(R.query) then
              T.query = R.query;
           else
              T.query = Base.query;
           endif;
        else
           if (R.path starts-with "/") then
              T.path = remove_dot_segments(R.path);
           else
              T.path = merge(Base.path, R.path);
              T.path = remove_dot_segments(T.path);
           endif;
           T.query = R.query;
        endif;
        T.authority = Base.authority;
     endif;
     T.scheme = Base.scheme;
  endif;

  T.fragment = R.fragment;
Community
  • 1
  • 1
Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
22

This can be achieved using JavaScript by setting the window.location.port property.

<a href="#" onclick="javascript:window.location.port=8080">go</a>
peterp
  • 3,145
  • 1
  • 20
  • 24
  • sure... but only in JavaScript. Try to make a relative StyleSheet-path for instance – Julien Schmidt Nov 29 '11 at 20:33
  • Not 100% sure of which context the question applies, but given the question I guess you could also serve the CSS from the same port. Then relative paths will be relative. – peterp Nov 29 '11 at 20:35
  • This only works if `href` contains only an anchor. See [my answer to another question](http://stackoverflow.com/a/13522508/60075) which has a similar solution but should work for other hrefs too. That is, modify `event.target.port` rather than `window.location.port`. – Craig McQueen Nov 23 '12 at 02:57
2

Simple answer: not possible. You need to use an absolute path if the host changes.

Julien Schmidt
  • 1,060
  • 8
  • 8
-5

Simply you can write in href attribute:

/:port/[path/]file.ext
j0k
  • 22,600
  • 28
  • 79
  • 90