index.php shows up in url

I run two websites on apache2 on ubuntu, site roots are in /var/www/site1/html and /var/www/site2/html.
The url structure of both of them is like this: example.com/index.php/%year%/%monthnum%/%day%/%postname%/

When I change it in wordpress settings, I get the apache2 404 page, not the wordpress 404 template, everywhere except at the frontpage. wp-admin still works, because in its url, there isn’t index.php.

mod_rewrite is enabled, I edited the .htaccess file, and also tried adding this:

to the .htaccess file, but the error still persists.

Also, if I set the url settings to plain (example.com/?p=123), it works, there’s no index.php in it.

At this point I have no idea what can cause it, I can only think of database, or file permission errors, every idea is welcome.

The settings that I want to work:
[1]: https://i.stack.imgur.com/c4gu3.png
The settings with which I dont get 404:
[2]: https://i.stack.imgur.com/i7K93.png