0

How to redirect a parameter like http://example.com/?link=https://google.com to https://google.com?

CHOO YJ
  • 151
  • 2
  • 5
  • 24
  • 1
    You can't do this with HTML alone. You will need javasctipt to do it client side or a server side technology of you are doing this as the result of a web request. Please clarify what you are trying to do. – Jon P Jul 01 '20 at 01:35

1 Answers1

1

Very simple way of achieving this redirect with Javascript

function isValidURL(string) {
  var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
  return (res !== null)
};

var urlParams = new URLSearchParams(location.search);
var redirectLink = urlParams.get('link');
if (isValidURL(redirectLink)) {
  window.location = redirectLink;
}

isValidURL function author: vikasdeep-singh

rafaelcastrocouto
  • 11,781
  • 3
  • 38
  • 63