Nope! You can do it without breaking a thing. All you have to do is convince your web server to look for PHP commands inside files that have file extensions other than .php.
If you are using the Apache server— and if you're working with PHP, Apache is by far the best choice— then it's easy to do this. Apache allows webmasters to change configuration settings on a per-directory basis. This is done by creating a .htaccess file containing commands that tell the server what should be done differently for that particular directory.
By creating a .htaccess file that lives in your web server's home directory (where your home page is), you can change how all pages are handled.
So to convince Apache that .html, .htm, and .asp and .aspx pages should be scanned for PHP commands, all we have to do is use the AddType command in a .htaccess file located in your website's home directory.
It's easy! Just create a text file containing this line:
AddType application/x-httpd-php .php .html .htm .asp .aspx
Now save that file with the name .htaccess and upload that text file to your web server's home directory.
Only this name will work. Not htaccess.txt, not htaccess (you must have the leading dot), and not .htaccess.txt. Note: all lower case!
<?php echo("If you see this, PHP is working!")?>
If you see the the message, you're all set. But if you see a blank page, don't panic! just follow these troubleshooting suggestions.
If It Doesn't Work
1. Double-check the filename. You did call it .htaccess with a leading dot, no .txt at the end, and no upper case... right?
2. Make sure the command you put in the file matches what I recommended above.
3. Make sure you put the file in your website's home directory, where your home page is located.
4. Your server does support PHP, right? Double-check that by creating a regular PHP page with a normal name like test.php. If PHP code in that file doesn't work, you need to take a step back and get a PHP-enabled web server before you follow along with this article.
5. It is possible that your Apache server is not configured to allow .htaccess files to make this kind of change. If so, you can still do it— but you will have to add the AddType line above to your Apache httpd.conf file. On Red Hat Enterprise Linux, you would edit the file /etc/httpd/conf/httpd.conf. Then signal Apache to reload its configuration file with the command:
service httpd reload
Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!