Search code examples
phphttp-redirectheader

Having redirect issue


I'm using directory structure (for redirecting from other pages for example after succesful registration) with header("Location: ../../../index.php"); function. It works in local server well but in remote one getting error message

Now, If I put localhost/ it will work in my local webserver but not in remote one. If I put the exact address of the web page then it will not work in my local server. How can I generate websites (main pages) URL with PHP? Is it possible?


Solution

  • use this:

    header('Location: http://'. $_SERVER['HTTP_HOST'] . '/');
    

    This will take you to the homedirectory of the server (note, if running locally you need to have your virtualhosts configured correctly but not hard to do).

    If you need to direct to a specific directory or file, just add after the '/' above.