0

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.)

2
  • This question is similar to: PHP - Make .txt file executable on apache. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 6 at 20:22
  • Are you sure .htaccess is enabled and being read? Did you try the answer in the duplicate I linked? There are other duplicates with the same answers, eg stackoverflow.com/questions/32632921/… Commented Jul 7 at 22:43

2 Answers 2

2

You do not need rewrite. Try this config:

<Files "test\.txt">
    ForceType application/x-httpd-php
    # or
    # AddHandler application/x-httpd-php .php
    # or
    # SetHandler application/x-httpd-php
</Files>
Sign up to request clarification or add additional context in comments.

2 Comments

That would be the relevant documentation: httpd.apache.org/docs/2.4/mod/core.html#forcetype
I tried all three versions, at the top and the bottom of .htaccess. Six total combinations. It did not make any difference: I still see the PHP source code when viewing the txt file.
0

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>

1 Comment

Great you solved the problem. It seems your real question was "how to find the name of the PHP handler on my host". There are duplicates of that here already too, eg serverfault.com/a/953141/479832, stackoverflow.com/a/49375772/6089612. If you want to make these problems easier to solve for future visitors, please consider closing this question as a duplicate, and upvoting those questions/answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.