WWW FAQs: How do I switch to PHP without breaking all my links?

2007-02-13: "I want to use PHP in my web pages. I'd like to add PHP commands to my existing HTML pages, or possibly switch from ASP to PHP. But won't that break all of my existing pages?"

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.

Double-check that the file is really called: .htaccess

Only this name will work. Not htaccess.txt, not htaccess (you must have the leading dot), and not .htaccess.txt. Note: all lower case!

Next, add a PHP command to a .html, .htm, .asp or .aspx file if you haven't already, and try refreshing that page with your browser. You should see the results of your PHP code! If not, create a simple test page called mytest.html with just this code in it:

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

Of course, you can't just shove PHP code into existing ASP code. You need to migrate completely from ASP to PHP. When I suggest "adding PHP code to an ASP file," I am assuming that you have already committed to rewriting the ASP code in PHP, and you just want to know how to solve the problem of broken links to files with a .asp extension.

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

Legal Note: yes, you may use sample HTML, Javascript, PHP and other code presented above in your own projects. You may not reproduce large portions of the text of the article without our express permission.

Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!