'PHP $_GET is empty when the url has variables passed

this is my first stack overflow question. I have a home page, where you can login, if you login it sends you to a login php in which you have some code that checks if login is incorrect. If that is the case this code redirects you back to home page:

header("Location: ".SERVER_ADDRESS."home?error=invalid_login");

Now you are back on the home page but the url variable is not set. It does not show in a var_dump(parse_url($_SERVER['HTTP_REFERER'])); or var_dump($_GET);

This is my index.php file:

<?php

session_start();
require_once("config.php");

if(!(isset($_GET['page']))) { // redirect to main page 
    
    header("Location: ".SERVER_ADDRESS."home");
    
} else {
    
    $mp = new MainPage($_GET['page']); // redirect to given page
    
}

?>

If you go try login again with wrong credentials and do the same redirection process it will now show the variable. Same thing happens if I were to refresh the header after few seconds.

I seem to not understand why the home page would not show the variable straight away, I just want to use the variable for an if statement that would print an extra invalid login message.

I am using wampserver64 for localhosting.

My .htaccess looks like this:

Options FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9a-zA-Z\-\#]+)/?$ index.php?page=$1 [L]

EDIT: CBroe's answer helped me, my htaccess did not have a QSA flag



Solution 1:[1]

Your rule matches on home, and rewrites the request to /index.php?page=home.

You need to add the QSA flag - query string append - to get the new query string you are creating there, merged with any existing one.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CBroe