Writing links without including a protocol

I have a website that can be viewed in both http and https protocols, the problem is that the links and assets should reflect the protocol being viewed by the website.

One solution would be to use relative links, however for some reason I cannot use relative links, as a result I had to find an alternative ...

I recently found out that you can write such a link to use the current protocol:

 <a href="//www.example.net/test/">Test</a> 

So far I have used the following:

 <?php // Get the current protocol $_PROTOCOL = $_SERVER['HTTPS']=='on' ? 'https://' : 'http://'; ?> <a href="<?php echo $_PROTOCOL; ?>www.example.net/test/">Test</a> 

While the first solution is working, I only recently discovered that he is not familiar with how it works and whether it is reliable. I know that older browsers may not like this, but this does not bother me, since these are only very old browsers (apparently).

When I tried to create a sitemap for one of my sites, the URLs were written very strange:

 http://beta.example.net///venue/ 

Instead

 http://beta.example.net/venue/ 

Why is this? Is it because of the links I use?

Is my original (PHP) solution better despite the large amount of code to go with it?

+4
source share
3 answers

You can use "//example.net" anywhere you want to use a regular URL, and the format is in accordance with Β§4.2 standard. RFC 3986.


However, if you still want to change the URL scheme in its purest form, you can use http_build_url :

http://php.net/manual/en/function.http-build-url.php

like this:

 http_build_url("http://yoururl.org", array('scheme'=>'https')); // changes to https:// 
+3
source

Incorrect links in the sitemap due to the fact that the generator is erroneous. The best solution is to update the Sitemap generator so that it works as it should. A simple solution is to use the $ protocol.

0
source

You did not specify a code that reproduces the problem you described, with links showing additional slashes. I assume that the code you use is trying to collect the entire URI, and there are trailing / leading slashes in the parts you concatenate.

The $ _SERVER ['HTTPS'] parameter cannot be set to "on" so you know the specific behavior. It just set it to false according to the manual , but that should not be a problem in the code you provided here.

Using a relative protocol URL is certainly preferable if PHP does the extra work of rebuilding your URIs with each request and is also error prone. I would recommend using relative protocol URLs if you think they are necessary, but as a rule, you are advised to build your site from scratch in order to use HTTPS or not. Relative protocol URLs may still cause some browsers (such as IE) to issue a warning or inform the user about an SSL violation for the page.

You should always check these things and test them carefully when you need a safe transfer. It is easy to accidentally slip into http://foobar.com/img.jpg or inadvertently send an AJAX request via HTTP instead of HTTPS, and suddenly the SLL icon for this page is now in the user's browser.

0
source

All Articles