How to redirect a parameter like http://example.com/?link=https://google.com to https://google.com?
Asked
Active
Viewed 65 times
0
-
1You 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 Answers
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