Method 2
Following the concept of method 1 (below), I realised this could probably be done with just one technology and without reloading the current page. It's all done client side with plain javascript.
<!DOCTYPE html>
<head>
</head>
<body>
<a href="https://validator.w3.org" id="allowReferrer">Link that you do want to allow referrer to be passed to</a>
<a href="http://w3.org">Link that you don't want referrer to be passed to</a>
<script>
document.getElementById('allowReferrer').addEventListener('click', function(e){
e.preventDefault();
var metaRef = document.createElement("meta");
metaRef.setAttribute("name", "referrer");
metaRef.setAttribute("content", "origin");
document.head.appendChild(metaRef);
window.location.href=this.href;
});
</script>
</body>
</html>
(This code can definitely be improved - its just a proof of concept.)
Esentially there is an event listener on the allowReferrer
ID which when triggered prevents the link to be followed, adds the referrer = origin meta tag to the head, then follows the link. Much tidier than method 1.
Method 1
I've just tested a rather convoluted solution, but its the only solution I can think of, and it did work.
<!DOCTYPE html>
<head>
<?php
if ($_GET["referrer"] == "true") {
echo '<meta name="referrer" content="origin">';
echo '<script>window.location.href = "'.$_GET["location"].'";</script>';
} else {
echo '<meta name="referrer" content="no-referrer">';
}
?>
</head>
<body>
<a href="http://example.com/?referrer=true&location=https%3A%2F%2Fvalidator.w3.org">Link that you do want to allow referrer to be passed to</a>
<a href="http://google.com">Link that you don't want referrer to be passed to</a>
</body>
</html>
(This code can definitely be improved - its just a proof of concept.)
When the page loads it looks for a query parameter called 'refferer', if that is not set to "true" then the referrer meta tag is set to 'no-refferer', but if it is set to "true" then the referrer meta tag is set to 'origin' and javascript is executed to redirect you to a location held in second query parameter called 'location'.
All links will normally be followed without passing any referrer information. If you want a link to allow the referrer to be passed, then you would create your link as
[current URL]?referrer=true&location=[link which should be url encoded]
for example
http://example.com/test/some-page.php?referrer=true&location=https%3A%2F%2Fvalidator.w3.org
Its definitely not pretty, but until there is good support for the referrerpolicy attribute on the anchor tags, it might be the only way. Before using this solution, I would urge you to ask yourself how important it is that you have this functionality, and is it worth it?
https://validator.w3.org/check?uri=http://www.example.com/path/to/foo
.