Reputation: 4838
I am creating a small internet site for my personal stuff. I want to put there a few links to e.g. FTP resources or SVN server. The important thing is that the FTP server has the same IP address as the page. I don't want to hard-code the address of my site in the link, because I consider this an anti-pattern. Instead, I would like to tell browser that the resource is on the current server, whichever server it is.
Let's say that the current page is https://example.com/stuff/index.html
. If I create a tag <a href="/things/index.html">things</a>
, it will lead to https://example.com/things.index.html
.
However, if I add a protocol identifier to an URL, it won't work. For example, <a href="ftp:///files/thingies.tar.gz">download</a>
will lead to ftp:///files/thingies.tar.gz
, not to ftp://example.com/files/thingies.tar.gz
.
What magic code should I put in the place of question marks:
<a href="ftp://???/files/thingies_directory">download thingies</a>
UPDATE: I would prefer a client-side solution. My server machine has very low processing power and RAM amount.
Upvotes: 0
Views: 463
Reputation: 16243
In php (server side language code) if you'd like to forward
ftp:///files/thingies.tar.gz
to
ftp://example.com/files/thingies.tar.gz
considering example.com is the domain where your server is hosted, just do
echo 'ftp://'.$_SERVER['HTTP_HOST'].'files/thingies.tar.gz';
or, in your specific case
<a href="ftp://<?php echo $_SERVER['HTTP_HOST'] ?>/files/thingies.tar.gz">download thingies</a>
Upvotes: 1