Search code examples
php.htaccessurl-routing

.htaccess denying $_GET params


Basically, I'd like to pass a $_GET param like that:

<a class="btn btn-danger" href="/deleteController.php?delete=<?= $tasks['listid'] ?>"> Delete </a>

However, it seems that my .htaccess file does not see it as a valid file:


Warning: require_once(/opt/lampp/htdocs/src/controller/deleteController.php?delete=1): failed to open stream: No such file or directory in /opt/lampp/htdocs/public/index.php on line 14

Fatal error: require_once(): Failed opening required '/opt/lampp/htdocs/src/controller/deleteController.php?delete=1' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/public/index.php on line 14

And finally my .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L]
</IfModule>

If someone could help me figuring out how can I get my .htaccess to allow $_GET params, and still requiring the wanted file, I'd greatly appreciate it.

EDIT:

index.php controller (before the fix):

$uri = urldecode($_SERVER['REQUEST_URI']);

   if ($uri === '' || $uri === '/' || $uri === '/index.php') {
       $uri = '/task_list_Controller.php';
   }
   require_once(CONTROLLER_PATH . $uri);

Solution

  • Your problem not related to Apache.

    Warning: 
    require_once(/opt/lampp/htdocs/src/controller/deleteController.php?delete=1): 
    failed to open stream: 
    No such file or directory in /opt/lampp/htdocs/public/index.php on line 14
    
    Fatal error: 
    require_once(): Failed opening required '/opt/lampp/htdocs/src/controller/deleteController.php?delete=1'
    

    You're trying to require_once(.../deleteController.php?delete=1) which of course will not exist, since the file deleteController.php?delete=1 does not exist. there is only deleteController.php.


    github

    In file /opt/lampp/htdocs/public/index.php on line 14 I saw that You're requiring file by CONTROLLER_PATH.$uri.

    So the problem comes from line 9 where You're passing uri with query string.

    simply write it like this:

    $uri = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
    
    if ($uri === '' || $uri === '/' || $uri === '/index.php') {
       $uri = '/task_list_Controller.php';
    }
    
    if (is_file(CONTROLLER_PATH . $uri)) {
      require_once(CONTROLLER_PATH . $uri);
    }