Running PHP On Non-.php Web Pages
Sometimes, a person wants to run PHP on certain web pages.
Yet, the hassles of renaming pages, what with breaking
incoming links and putting up redirects for search engine
spiders, can be a daunting task.
Here is the good news. On many servers, if not most, it is
possible to run PHP scripts on web pages with .html, .htm,
and even .shtml file name extensions.
With every Unix/Linix hosting account we've had, it could be
done. However, the ability to do what I outline in this
article can be blocked with the server's configuration file,
which some hosting company may have decided to do. (The
method probably will not work on Microsoft IIS servers.)
The method consists of putting 2 lines in the .htaccess
file. If your server is configured to disallow it, the lines
will simply have no effect.
Before I go into what those two lines are, let me mention
something that is very important:
>>> Your .htaccess file might be hidden!
Please note: Some FTP programs won't list files beginning
with a period. If a .htaccess file is already on your
server, even if you don't see it in the FTP listing, and you
proceed to upload another .htaccess file, you will overwrite
the file already on your server.
If you are unsure, test your FTP program by uploading a file
with a name that begins with a period, .test for example,
then refresh the file listing and see if the file .test is
included.
Considerations:
-
If your FTP program's preferences allow you to
specify that files beginning with a period shall
be displayed in the file listings, make it do so.
-
If your FTP program allows you to type the file name
that you want to download (instead of selecting the
file name in the file list), you could try
downloading the .htaccess file that way. If
the file downloads, it was available. If it
does not, the file is not there.
-
Some hosting companies provide a control panel that
contains a "Files" or similar link that will display
the files on your server and let you up/download. If
the control panel displays file names beginning with
a period, it can be used to download the current
.htaccess file (if there is one).
Also, note: Files beginning with a period might not show on
your computer's listings, depending on which computer you're
using and your preference settings. Test it, to verify.
If they don't already, settings will need to be changed
until the files do show up. Otherwise, is likely an existing
.htaccess file will be overwritten.
Now that you know whether or not there is an .htaccess file
already on your server, and that the .htaccess file will
display with your computer's file listings, here are the
instructions for the two .htaccess lines.
But first, if your server already had a .htaccess file, make
a backup copy of it. That is in case you want to revert.
Okay, we're back.
If you already have a .htaccess file, add these two lines to
it. Otherwise, create a new .htaccess file with the lines:
AddHandler application/x-httpd-php .html .htm .shtml
AddType application/x-httpd-php .html .htm .shtml
The above two lines tell the server to process .html, .htm,
and .shtml files for PHP code.
If you only need one or two file types for PHP code, remove
the file name extensions of the ones you don't need. When an
extension is removed from one line, it must also be removed
from the other.
Upload the new or revised .htaccess file to your server.
Test a web page to verify PHP works as it should.
Put some PHP on a test web page and load it into your
browser. If you have nothing else handy, this can be used:
<?php
echo('<span style="font-size:1in">Hiya!</span>');
?>
If a 1-inch tall word "Hiya!" appears on your web page, the
PHP is working. Otherwise, the PHP is not being processed or
something is incorrect with the PHP code on the test page.
Will Bontrager
©2007 Bontrager Connection, LLC Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.