Why is the the PHP code executed in both files in a single request?

Viewed 39

I spent a whole day debugging a single issue which boiled down to this behavior. It doesn't make sense to me.

I have 2 files index.php and session_plus.php.

Both simply increment a session variable by one for each request.

When I add an .htaccess file with rewrites...

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

and hit session_plus.php the session variable is incremented twice. Once by index.php and another time by session_plus.php. When I remove the .htaccess file the request increments once as expected.

I thought the RewriteCond %{REQUEST_FILENAME} !-f rule would make a request bypass index.php completely.

Can someone please explain what is happening here?

Here is the example: http://0image.net/

Here is my code: https://github.com/websitedesignby/0image.net

1 Answers

This probably isn’t actually just a single request, as you thought.

More likely, it is something like the browser automatically making a request for /favicon.ico (as they often do by default) - and if you don’t have such an icon file uploaded to begin with, then this request will of course be routed to your index.php in this situation here, due to the rewriting you configured.

The browser dev tools’ network panel is always helpful to check what requests are actually happening.

Related