I have a file in my theme’s folder called “test.php”.
If I go to www.mysite.com/wp-content/themes/my-theme/folder/test.php the theme loads which I assume is a 404 error handled by WP. If I change the file name to be test.html I can access it no problem.
I tried adding the following code to my functions.php file (to add a rewrite rule):
add_rewrite_rule( 'test.php$', $relative_directory . '/folder/test.php [L]', 'top' );
but even having the htaccess rule didn’t seem to do anything.
Any idea what could be causing this?
Assuming you use the correct URL to access the file, everything is being handled in your web server level and WP doesn’t run at all, so WP based solutions like changing rewrite rules will not help you.
It is most likely that there is some web server configuration that sends the 404 for any access to a php file at that folder (guess you should try if other php files there also return 404). You might be able to fix the web server configuration to match your needs, but as @EAMann commented, it is not the right way to write ajax for WP.