Processing HTML Pages As PHP

Phillip created popular HTML pages that I wanted to add PHP code to, to track the number of hits they’ve been getting. I didn’t want to change the file’s extension or use redirection, so I wondered if it was possible for Apache to treat HTML files as PHP files; it is: add the following to your httpd.conf file:

AddType application/x-httpd-php .htm
AddType application/x-httpd-php .html

Works like a charm. You could use that technique to hide the fact that your website is using PHP to process your pages.

Preventing Image Bandwidth Theft Using Apache

I recently noticed this site’s images were getting a lot of hits – only the images, not the pages they were posted to. I discovered a lot of users of forums and chats linking directly to the images on this site, instead of copying the image and hosting it on their own website. This type of image linking is a drain on my poor server, and it’s considered bandwidth theft.

Here’s the code in my Apache httpd.conf file to prevent websites from “stealing” my images:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?steelwhitetable\.org [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?cairns.servehttp\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?bloglines\.com [NC]
RewriteRule .(gif|jpg|png)$ - [F]

This prevents .gif, .jpg., and .png files from being displayed on any website except those specified. So, if I wanted to allow to display my images, I would add the following to the above:

RewriteCond %{HTTP_REFERER} !^http://(www.)?google/.*$ [NC]

You could even redirect those attempting to use a direct link to an image on your website to a different image. Add the following before the RewriteRule, where “bad.jpg” is the image that will be displayed on other websites:

RewriteCond %{REQUEST_URI} !^/bad.jpg$ [NC]