I have a single, specific non-PHP file on the server that must be executed as PHP. For the sake of example let's say it's a text file called test.txt
and its contents are: <? echo 'hello world'; ?>
So, when a site visitor accesses test.txt
, they should not see the contents of the text file: instead, the server should execute the text file as PHP, and the visitor should see hello world
.
No other text file on the server must execute as PHP, but only this specific one.
I tried the following in my .htaccess
file:
RewriteEngine On
RewriteRule ^test\.txt$ - [H=application/x-httpd-php]
No effect: I still see the contents of the text file (i.e. the PHP source code instead of the output).
My PHP version — as determined by echo phpversion();
— is 8.1.32. But it still doesn't work if I change the x-httpd-php
type to add a version number: I've tried x-httpd-php5
and x-httpd-php8
and x-httpd-php81
. (From reading other answers on this site, there seems to be no reliable way to find out exactly which number to put here.)
How can I get the text file to execute as described above?
(As for why I want to do this: I'm trying to find a workaround for my other unsolved issue here, relating to WooCommerce shop limitations on product download URLs.)
Okay, I have finally found a configuration that works.
I took the value from PHP of $_SERVER['REDIRECT_HANDLER']
which is application/x-httpd-ea-php81
. (So unfortunately it seems I will have to change this .htaccess rule every time that the PHP version gets updated...?)
Then I put this into the .htaccess file:
<Files test.txt>
AddType application/x-httpd-ea-php81 .txt
</Files>