Reputation: 171
How to redirect a parameter like http://example.com/?link=https://google.com to https://google.com?
Upvotes: 0
Views: 75
Reputation: 12161
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
Upvotes: 1