I have a website that can be viewed in both the http and https protocols, the problem here being that the links and assets must reflect the protocol that the website is being viewed over.
One solution would be to use relative links, however due to particular reasons, I cannot use relative links, resultantly, I have had to find an alternative…
I have recently learned that you can write a link like so in order use the current protocol:
<a href="//www.example.net/test/">Test</a>
Up until now I have been using the following:
<?php
// Get the current protocol
$_PROTOCOL = $_SERVER['HTTPS']=='on' ? 'https://' : 'http://';
?>
<a href="<?php echo $_PROTOCOL; ?>www.example.net/test/">Test</a>
Whilst the first solution works, I have only recently discovered it am not familiar with how it works and whether it is reliable. I am aware that older browsers may not like it, but this does not bother me as it is only very old browsers (apparently).
When I tried to generate a sitemap for one of my websites, the urls were written very oddly like so:
http://beta.example.net///venue/
Instead of
http://beta.example.net/venue/
Why is this? Is this because of the links I am using?
Is my original (PHP) solution better, despite the large amount of code to go with it?
You can use “//example.net” anywhere you want to use a regular URL, and it’s a format according to the standard §4.2. RFC 3986.
However, if you still want to change the scheme of a URL in a clean manner, you can use
http_build_url:http://php.net/manual/en/function.http-build-url.php
like this: