.htaccessmod-rewrite

I call the PHP file in the subfolder, but the .htaccess uses a weird rewrite


In the root I have files: .htaccess and page.php In the /test/ subfolder I have files: define_session.php and write_session.php

.htaccess:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)?$ page.php?p1=$1&p2=$2&p3=$3 [L,QSA]
# www.example.com/parameter1/parameter2/parameter3/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)?$ page.php?p1=$1&p2=$2 [L,QSA]
# www.example.com/parameter1/parameter2/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page.php?p1=$1 [L,QSA]
#www.example.com/parameter1/

page.php

<?php
echo "Hello World";
// session_start();
// session_destroy();
?>

/test/session_define.php

<?php
session_start();
$_SESSION["session_test"] = "123456789";
?>

<a href="session_write.php">session_write.php</a>

/test/session_write.php

<?php
session_start();
echo "session_test:".$_SESSION["session_test"]."<br>";
?>

OK scenario:

  1. In Google Chrome, I call the URL "domain"/test/session_define.php
  2. I click on the link and I get to the "domain"/test/session_write.php page and I see the SESSION variable "1234578" listed. Everything ok.

Error Scenario (in page.php I uncomment "delete sessions"):

  1. In Google Chrome, I call the URL "domain"/test/session_define.php
  2. I click the link and it goes to "domain"/test/session_write.php, but $_SESSION["session_test"] is not defined (it is removed because of page.php).

I am calling a specific php file (URL) in a subfolder and I thought that the condition could not be met: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ page.php?p1=$1 [L,QSA]

I don't understand how page.php can remove SESSION when I call PHP file "/test/session_define.php" directly. There should be no rewrite using .htaccess. In page.php, I have also intentionally stated: echo "Hello World"; but this doesn't show, only sessions are deleted.

Please what am I doing wrong? What am I getting wrong? Thank you very much for your help.


Solution

  • If a page doesn't explicitly specify the location of a favicon, some browsers try and request /favicon.ico by default.

    If you do not have such a file, this would trigger your last rule, and be rewritten to /page.php?p1=favicon.ico. So your session gets destroyed by that request.