PHP

ID #1

Can I have all .html pages parsed as PHP?

Applies to: Grid System

By default, files ending in .php and certain other extensions will be parsed as PHP and you do not need to do anything to make this happen.

If you want all .html files parsed as PHP, just add the following line to an .htaccess file in the same directory or any directory above the one in which you want this behavior:

AddHandler cgi-script .html

To also have all .htm files parsed as PHP, add .htm to the end of the above line. Nothing else is required if you want .htm or .html files to be parsed as PHP.

If you want any other filetype other than .htm or .html to be parsed as PHP, such as .wow then you will have to do as above:

AddHandler cgi-script .wow

plus you will have to make all your .wow PHP files world executable (chmod -R 755 *.wow) and put the following on the first line of every .wow file before any opening php tags:

#!/usr/bin/php

These 3 steps (AddHandler, chmod, and #!/usr/bin/php) are necessary when you want filetypes other than .htm and .html to be executed as PHP.


Last update: 2011-12-21 16:35
Author: FAQ Admin
Revision: 1.4

Digg it! Share on Facebook Print this record Send FAQ to a friend Show this as PDF file
Please rate this FAQ:

Average rating: 2.33 (3 Votes)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ

Comment of Anonymous:
#!/usr/bin/php doesn't seem to work but #!/usr/local/bin/php does.
Added at: 2002-09-17 10:46

Comment of Anonymous:
#!/usr/bin/php seems to only need to be used on the very first open php tag
Added at: 2002-09-17 13:26

Comment of Anonymous:
If you need an alternative to ForceType to parse extensionless files as php AND you are trying to use $PATH_INFO to carry query string variables (in order to avoid URLs with query strings), like:

http://www.yourdomain.com/extensionlessfile/varname/varvalue

where "extensionlessfile" is the filename of the php script that does the parsing of "varname", then you need to cd into the same directory as extensionlessfile and make a symlink from "extensionlessfile" to a file with a .php extension like this:

ln -s extensionlessfile extensionlessfile.php

Then use mod_rewrite to pass all requests for the above URL invisibly to the .php script by putting these rewrite rules in an .htaccess file in the same directory or above this directory:

RewriteEngine on
RewriteCond %{REQUEST_URI} !extensionlessfile\.php
RewriteRule ^extensionlessfile(.*)$ /extensionlessfile.php$1
Added at: 2002-10-16 10:30

Comment of Anonymous:
If you're trying to run a weird file extension that requires you to have

#!/usr/local/bin/php

at the top of each file, you can do that real quick to your entire file tree by doing this from a shell prompt:

cd /htdocs
cp -a www www-old
cd www
find . -type f -name \*.weirdextension -exec perl -pi -e 'print "#!/usr/local/bin/php\n" if($. == 1); close ARGV if eof' {} \;

That was 4 commands, 1 on each line hitting ENTER in between. The last one is a long one and should be all on 1 line even though it wraps around and looks like multiple lines in this note.

That will make a backup of your existing files first, and then find every file under your tree that is named like *.weirdextension and it will prepend the appropriate hash-bang line at the top of all those files.

Added at: 2004-03-07 03:41

Comment of Anonymous:
Using mod_rewrite is rarely useful, I find. This is mainly due to the fact that all requests need to go through Apache's file find functions twice. Once as 'testpage', which triggers the rewrite, and once as 'testpage.php', which is passed through the rewrite again, but does not trigger it. This is hardly efficient.

As for which #!/.../php lines work - it's entirely dependant on your installation of PHP. Some PHP installations will install the php executable in /usr/local/bin, some in /usr/bin, and some in more exotic locations (like /opt/php-version/bin) and some installations do not have a command-line version of PHP at all!

Having the first line of a file starting with #! and a path to an executable started long ago with shell scripts, and has continued to this day as a useful way of telling the system which program to dump the file into. Other common examples are #!/bin/bash, #!/bin/sh, #!/usr/bin/perl, #!/usr/bin/python, etc. If you use a first line of #!/bin/cat you will get out what you put in. Using programs like 'sed' you can create interesting (and blindingly fast) search-and-replace statements, and more.

An even better example would be to AddHandler cgi-script .xml and specify "#!/usr/bin/xsltproc -" as the first line. You can have a properly formatted XML document automatically converted to HTML this way. (Though I don't reccomend it, as this would be horribly slow. Try a combination of XML, XSLT, and PHP for cacheing.)
Added at: 2004-04-25 14:35

Comment of Anonymous:
The "ForceType" directive is unsupported here. In the future, Apache will not be compiled with PHP inside it as a module. At that point php will not be part of the httpd and so "x-httpd-php" will be a nonfunctional directive and will break any site that relies on it. That is why the mod-rewrite alternative to Forcetypes is suggested above.
Added at: 2004-04-26 16:30

Comment of Anonymous:
If you're trying to get rid of a ForceType directive for an extensionless file, and you've got a number of extensionless files in the same directory that all need to be parsed as PHP, then make the .php symlinks as per the above, but then use this rewrite rule:

RewriteEngine on
RewriteCond %{REQUEST_URI} /(file1|file2|file3|file4)$
RewriteRule .* %1.php

If the extensionless files are not at the DocumentRoot, then you'll need a RewriteBase directive. For example, if the extensionless files were under /htdocs/www/example (where /htdocs/www is the DocumentRoot), then you'd add this to the rewrite ruleset:

RewriteBase /example
Added at: 2004-12-16 13:15

Comment of Anonymous:
AddType application/x-httpd-php .html

is another way to do it.
Added at: 2007-08-25 02:23