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/

akash ujjwal
  • 387
  • 1
  • 4
  • 18

2 Answers2

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>
akash ujjwal
  • 387
  • 1
  • 4
  • 18
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();