Search code examples
phpxampp

How to do a redirect in php from a root url to another without getting a redirect error


currently I am trying to make my own website that functions in a way like a wiki. I am using the XAMPP installer to create it, and I formatted the index.php file to redirect to a request file without any errors: http://localhost/site/request.php

The request file, as the name suggests, is the file where a request is made in the url, such as a value, etc, which would correspond to what file/page the user is looking for. However one thing that I would like to do is if the request is empty, it will redirect to the main page: http://localhost/site/request.php?Main_Pages:Main_Page Main_Pages being the type of page that the user wants to see, while Main_Page being the specific page of that type. Anyways, I tried doing this by inputting this piece of code in the request.php file:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if ($url = 'http://localhost/site/request.php') {
  header('Location: http://localhost/site/request.php?Main_Pages:Main_Page');
  exit;
}

However when I try reloading the page, it redirects to that address successfully, but it gives me the browser error: ERR_TOO_MANY_REDIRECTS

If you guys could help me that would be great, Thanks.


Solution

  • Is the configuration set up on XAMPP correctly to allow redirects? Can you share the site/request.php code to also show us what is happening on that page?

    One more thing, your code says:

    ...
    if ($url = 'http://localhost/site/request.php') {
    ...
    

    which will be TRUE every time. You only have one =, it needs to be:

    ...
    if ($url == 'http://localhost/site/request.php') {
    ...
    

    if you plan on checking the actual value.