redirect html page when trying to enter from the browser

3

I need to avoid entering an html address, that is, if someone tries to enter from the browser, it directs it to the main page. On the other hand, it is only possible to access from the main page.

I would like to do it from htaccess, my js is the following

    $(".accept").click(function (event) {
  var counter = 0;
  $(".accept").each(function(key, checkbox) {
    if(checkbox.checked == false) {
      $("#purchase-continue").attr("disabled", "disabled");
      return false;
    } else {
      counter++;
      if(counter == $(".accept").length) {
        $("#purchase-continue").attr("disabled", false);
        }
    }
  });
});

$('#buy-token').on("#modal.close", function(event, modal) {
$(".accept").prop('checked', false);
$("#purchase-continue").attr("disabled", "disabled");
});

$("#purchase-continue").click(function() {
    switch($(this).attr("data-source")) {
        case "more-information":
            top.location.href = "information.html";
            break;

        case "buy":
            top.location.href = "instructions.html";
            break;
    }
});
    
asked by fedorqui 03.11.2017 в 17:24
source

2 answers

3

You can do this by reading the value of the referrer .

document.referrer will return a string with the URL of the page redirected to the current page. If the page was not accessed through a link (eg by typing the URL in the address bar directly or via a bookmark) its value will be an empty string.

Then in JS you could add something like this, that checks if the referrer is your main page, and if not, redirects to it:

if (document.referrer != URL_DE_TU_PAGINA_PRINCIPAL) {
    window.location.href = URL_DE_TU_PAGINA_PRINCIPAL;
}
  

WARNING : Although this method will work, it would be better to do a similar check on the server side because the user could disable JavaScript in their browser or edit it to remove that redirection and then you would skip your check.

    
answered by 05.11.2017 / 06:03
source
0

You can do it with a great library for that, it's called pagejs . It is a library for single page apps.

Or you can also try the following code. What it does is take the current address to validate it and then redirect.

let currentPage = window.location.href;
if (currentPage.indexOf('stackoverflow')) {
    window.location.replace('https://google.com.co')
}
    
answered by 04.11.2017 в 15:12