How to map another file extension to the php parser through htaccess

I was in the process of cleaning up my web hosting accounts when I came across one plan which had 2 sites on it. One of them was a static website (pure html) and the other one was written in classic asp. So I decided to move both sites to another hosting plan because it was just a waste of money as the sites did not have much traffic and there was no justification why the hosting was needed.

Since windows hosting plans are more expensive, I wanted to move the sites to a linux server and I knew I wouldn’t have any problems with the html website but I wondered how much trouble it would be to move the classic asp website. The code for the dynamic website was not using a lot of asp code as in only the files ended with an .asp extension but a php parser could very well serve the webpage. So after much googling, I found that adding a line to the .htaccess file would map any file extension to the php and that would do the trick for me.

Mapping asp to php parser

The line below will tell the php parser to treat files ending with .asp extension as if they were a php file.

AddType application/x-httpd-php .asp

How the above line did not work for me and I had a lot of problems trying to map the classic asp code before I found the solution. It has something to do with the version of php you’re using. Mine was 5.12.53 I think. I added the following lines in my .htaccess file and it worked:

AddType application/x-httpd-php4 .asp
AddType application/x-httpd-php5 .asp

Both lines are required. There’s something else that you need to do if you can’t get it to work. You can replace AddType with AddHandler (that depends if you’re running Apache as a module or CGI script). And sometimes you might need to remove the application word and have only this:

AddHandler x-httpd-php5 .asp

If you wanted to map another extension, you would do this:

AddType application/x-httpd-php5 .html .htm .me .newextension

Files ending with .html, .htm, .me and .newextension will then be run through the php parser and it would allow you to serve dynamic content through .html files or hide (disguise) the programming language you’re using on your website or make it easy to move from one coding language to another because if you don’t use a file extension reserved for a particular programming language, then you can easily switch to another programming language or server.

While mapping another extension, I also did two other things:

Define a default document (so that index.asp would be recognised as a default webpage for the folder/directory)

DirectoryIndex index.html index.htm index.php index.asp

Disable directory browsing (so that folders/directories without a default webpage are not listed)

Options -Indexes

Well so far so good, now just waiting for DNS propagation to complete before I can cancel the hosting plan.


Fixing the problem with Static Compression Module frequentHitThreshold

While trying to speed up my website, I struggled with getting the StaticCompressionModule to work on my local machine. By default, the static compression module is enabled so you would think that all your static files like javascript and css will be gzipped before being sent to your browser. However this is not necessarily the case as I found out with Chrome Developer Tools and the Firefox YSlow plugin.

Although the module is enabled, static files are only going to be compressed if they meet a certain criteria determined by the frequency of access controlled by two parameters (frequentHitThreshold and frequentHitTimePeriod). The first parameter is set to 2 and the second one to 10 seconds by default. This means that if a period of 10s, if you get 2 request for your static files then they will be sent as gzip. However this is not what I wanted and I needed to correct this behaviour.

It is possible to change the values for these parameters but when I put it in the web.config file, I got the following error:

This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault=”Deny”), or set explicitly by a location tag with overrideMode=”Deny” or the legacy allowOverride=”false”.

I googled the problem and thought I’d give the command line a try:

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/serverRuntime -frequentHitThreshold:1

Substitute your windir accordingly.

That worked great but I wanted to know why the web.config gave an error and it turned out that the config needed to be within a location tag  as follows:

<location path="Default Website">
 <serverRuntime enabled="true" frequentHitThreshold="1" frequentHitTimePeriod="00:00:10" />

Once the value of the frequentHitThreshold was changed to 1, I was able to see the content compressed before being sent out from the server to the browser. Sweet!