redirect page if it is entered from url

0

That I'm new to this and I need to redirect an html page if you try to enter from the browser, that is if someone tries to enter www.example.com/instructions, not possible but if possible from my homepage , validating an information, I'm trying to do it from htacces

something kind

if(checkbox==true){
top.location.href = "www.ejemplo.com/instructions.html";
}
else{www.ejemplo.com}
    
asked by 07.11.2017 в 16:45
source

4 answers

1

I would suggest using document.referrer , if you reached your page instrucciones.html using the address bar or from a bookmark, then it will be a string empty, otherwise (via a link) it would have to contain URL of the page that has linked this site, I give you an example:

instructions.html

if(document.referrer == ''){
    //Podrías redirigir a la página principal
    window.location.href = 'http://www.ejemplo.com';
}
else{
    //Si no se llegó a 'instrucciones.html' desde la barra de direcciones
    //Podrías verificar de qué página la trajo aquí
    if(document.referrer == 'http://www.ejemplo.com'){
        //Si se accedió desde tu página principal
    }
    else{
       //En caso que no, podrías redirigir a tu página principal, o mandarlos atrás usando 'history.back()'
        window.location.href = 'http://www.ejemplo.com';
    }
}

I hope and serve you.

    
answered by 07.11.2017 / 19:03
source
2

Send a parameter in the url for example on your page 1 from main page www.example.com/instructions.html?accesso=1

with java script what's valid

var url_string = window.location.href
var url = new URL(url_string);
var c = url.searchParams.get("accesso");
// Valida si el parametro viene en la url si no viene lo manda a la pagina principal
if (!c){
  window.location.href = "/"
}

If the browser enters the URL directly, it does not have the parameter by which it will be sent to the main page

    
answered by 07.11.2017 в 16:59
1

In .htaccess you could add something like this:

  

Clarification: I could not prove this solution and it could contain errors

RewriteEngine On
RewriteCond %{HTTP_REFERER} !http://www.ejemplo.com [NC]
RewriteRule ^/instructions.html$ http://www.ejemplo.com [L,R]

What it does line by line:

  • Activates the redirect and rewrite engine.
  • Check the HTTP_REFERER header to see if it's link (exactly that URL, the one on the homepage).
  • If the URL is instructions.html (and it did not come from the main page, condition of the previous line), then redirects to the home page link .
  • One problem with this solution is that the HTTP_REFERER header is not reliable: some browsers do not send it and a knowledgeable user could modify it and send it confusing to the server.

        
    answered by 07.11.2017 в 20:29
    0

    The solution of @luisdmz is the most practical and simple, I recommend that. Another solution may be to use localStorage . When you click on the link instructions put a flag so that neither page is loading, check if the flag exists and if it does not exist, redirect or return to where it was by history.back() .

    On the index.html page:

    const instructions = document.getElementById('#instructions');
    instructions.addEventListener('click', function() {
      localStorage.setItem('navigationValid', flag);
    });
    

    On page instructions.html

    const navigationValid = Boolean(localStorage.getItem('navigationValid'));
    if (!navigationValid) {
      history.back();
    }
    localStorage.removeItem('navigationValid');
    

    Instead of a parameter with a deductible name you can use a hash so that the user can not put the parameter in the URL or in the storage.

        
    answered by 07.11.2017 в 17:10