3

Using Apache it's easy to enable PHP for all files of any extension:

AddHandler application/x-httpd-php .css .js .txt .xml

How do I enable PHP for only a specific file but not all other files with that same extension?

I have tried AddHandler application/x-httpd-php test.xml and it did not work and the search engines aren't coughing up anything remotely relevant.

1
  • I don't know what are you trying to do, but another option is .htaccess rewrite. Commented Sep 17, 2015 at 14:34

2 Answers 2

2

AddHandler can be used inside a <Files> container.

AddHandler Directive

  • Description: Maps the filename extensions to the specified handler
  • Context: server config, virtual host, directory, .htaccess

... where directory stands for:

directory

A directive marked as being valid in this context may be used inside <Directory>, <Location>, <Files>, <If>, and <Proxy> containers in the server configuration files, subject to the restrictions outlined in Configuration Sections.

And <Files> can be used from .htaccess just fine:

<Files> Directive

  • Description: Contains directives that apply to matched filenames
  • Context: server config, virtual host, directory, .htaccess
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please elaborate and I presume this will work in an .htaccess file and not the direct server configuration?
I've updated my answer. Feel free to ask if something in the documentation is unclear.
0

Simply:

<Files hello.html>
AddType application/x-httpd-php .html
</Files>

The file tag selects only hello.html,the AddType then adds php support for all files it can "access". Due to the Files header, it can only "access" hello.html, and so only enable php for this one.

1 Comment

Please add some explanation to your answer such that others can learn from it

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.