1

I want to match a string in URL and redirect my page to other URL. Current URL is: http://example.com/?healing=f29c

Here is my code:

<script type="text/javascript"> jQuery( document ).ready(function($) { var redirect_url = 'example.com/healing/'; var current_url = window.location.href; if (current_url.indexOf('?healing=')){ if(!current_url.match(redirect_url)){ window.location.replace(redirect_url); } return false; } }); </script> 

But I'am not getting the proper output. It start redirecting other pages to with or without having '?healing=' string in their URL.

And their is recurrence of URL to example http://example.com/product-category/aromafrequencies/example.com/healing/

2 Answers 2

2

You have to do

<script type="text/javascript"> jQuery( document ).ready(function($) { var redirect_url = 'example.com/healing/'; var current_url = window.location.href; //please check condition if (current_url.indexOf('?healing=') > 0){ if(!current_url.match(redirect_url)){ window.location.replace(redirect_url); } return false; } }); </script> 
2
  • +1 to come closer to the answer but it remain have the recurrence issue. Commented Jan 5, 2016 at 0:26
  • on which url you are facing recurrence issue? Commented Jan 5, 2016 at 4:39
0
function RedirectUrl() { var expression = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/ var regex = new RegExp(expression); var current_url = window.location.href; var redirect_url = "**your redirect page url call here**"; console.log(current_url); if (current_url.indexOf('/news') > -1) { where news is the word which is present in the url eg.www.abc.com / news.html if (!current_url.match(redirect_url) && current_url.match(regex)) { window.location.replace(redirect_url); } } 

}

call this function where you need it during jQuery|Javascript on click event.

RedirectUrl(); 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.