Saturday, May 26, 2007

Custom extenstion for a Server Script

Intro
PHP files have a ".PHP" extension and is by default the extension used to run php scripts. Thats ordinary. Now it would be cool if we could name another extension (say .do or .html) for a PHP script and get the same output. This is precisely what the following is about.

In order to map a particular extension to a particular application, apache uses handlers. Basically you can define an extension and add the corresponding handler. This can be done in 2 ways :

CPanel / Apache Handlers
Add extension html and handler application/x-httpd-php to make a new handler :

html application/x-httpd-php

This makes any ordinary html file to be handled and processed by the PHP engine before it is output.

.htaccess
For those who dont have cPanel access, the .htaccess file can be edited.
Add
AddHandler application/x-httpd-php html

this line to your .htaccess file. This makes the same effect as done in the previous method.

Tip
  • Adding html extension as php can be cool, because a statically appearing page is actually dynamic. But the disadvantage of using this is that, all HTML files will be passed through the PHP engine and parsed for the PHP codes. This puts unnecessary pressure on the servers and makes it slow.
  • It is better to choose someother extension, if there is a lot of static content on your website. If most of your pages are in PHP, this effect is cool.
  • If for some reason your .htaccess gets deleted or restored to default, All the PHP codes will be echoed to the users browser and all the source would be revealed!. So beware when using this.

0 comments: