Apache ForceType application/x-httpd-php and PHP CGI

To display “static” pages here I use the Apache “ForceType” directive so that all requests to /pages/something get processed by a php file called “pages”. Unfortunately that doesn’t work as easily when PHP is running as a CGI.
I found a possible solution in this Textpattern forum thread. Not sure if you can put “ScriptAlias” into a .htaccess file. I just checked and no, you can’t. That limits the usefulness of this solution dramatically!
Here’s how to use PATHINFO to create search engine friendly URLs. Again, it uses the ForceType idea.
Looks like it’s worth perceivering with mod_rewrite after all!

By Donncha

Donncha Ó Caoimh is a software developer at Automattic and WordPress plugin developer. He posts photos at In Photos and can also be found on Twitter.

3 replies on “Apache ForceType application/x-httpd-php and PHP CGI”

I’ve been playing with Textpattern and encountered the problem you were describing here. I came up with an .htaccess solution that seems to work pretty well:

AddHandler php-cgi .php
Action php-cgi /cgi-bin/php.cgi

ForceType php-cgi

The first two lines are because I rolled my own PHP CGI and installed it. (I’m not using Dreamhost’s CGI.) The next section is from the Textpattern sample .htaccess, but I modified it.

If I had left it to say “ForceType application/x-httpd-php” it would run PHP as a module, which isn’t what I want. If it says “Force Type application/cgi-bin” it runs it as the default CGI. Still not good. On a hunch, I changed it to “ForceType cgi-bin”, and that worked.

It seems the ScriptAlias command isn’t necessary to get it working after all. I now have SEF URL’s on my site running PHP as a CGI.

Nice bit of work James, and useful. In the last 2 weeks WPMU (the software that runs this site) has gone through an extensive reworking of the mod_rewrite rules, with some pain I can tell you!
I think the rules are just about there now and give an added benefit. All “unknown” requests are sent through mod_rewrite now, so what I may do, right at the end of my mod_rewrite ruleset is have a catch-all rule that sends requests to my solitary “pages” script, or in other words, it does the same thing as the ForceType stuff but should be more portable I hope!

[…] minik bi araþtýrma yaptým .htaccess ile alakalý biþeymiþ, php yi cgi olarak çalýþtýrýnca /biþey/sayfa gibi statik sayfalarda sorun meydana gelebilirmiþ ve phpde bi bugmýþ… mod_rewrite kullanýyosan onunla direk baðlantýlýymýþ… Apache ForceType application/x-httpd-php and PHP CGI at Holy Shmoly! bu sayfadan biþeler buldum belki bi yararý olur… __________________ Running around, nowhere to go… […]

Leave a Reply