02 May 2007

 

How do I get Apache to treat URLs with no file extension as PHP?

Feel free to ignore this if you're not a web development type. If you are, it should be a simple question, but I don't know the answer.

I'm re-doing this website, as you've seen, and I'm using some PHP includes. On pages that end with a .php extension (instead of .html or something else), that works great. It's why you see the navigation sidebar and the footer at the bottom of the page on the home page and monthly archives (they're really only single files that get sucked into each page as it loads):

http://www.penmachine.com/index.php
http://www.penmachine.com/old/2007_05_01_.php

But for some reason, Blogger is generating individual post pages without extensions, like this:

http://www.penmachine.com/2007/05/dont-cheese-off-geeks
http://www.penmachine.com/labels/encryption

By default, the Apache web server software I'm using treats these files as plain text (showing all the code gobbledegook), but I want them to be treated as PHP, so I tried editing the .htaccess file thus:

DefaultType application/x-httpd-php

But that creates a 500 Server Error. Strangely, this works:

DefaultType text/html

So you see the pages above properly as HTML web pages, but of course they don't have the sidebar includes, because Apache doesn't know they're supposed to be PHP.

What am I doing wrong? Is there a better way to approach this problem? All I want is for Apache to say, "Ah, a URL with no file extension—that's PHP," then the rest should Just Work, as it does for files ending in .php.

Suggestions?

Labels: , , , ,


Comments:

You can do that with using RewriteEngine feature in Apache. It basically sets rules for redirecting HTTP requests internally with Regular Expressions. But I am guessing that what you need is already in WordPress though.
 
There's almost certainly a way to do it with mod_rewrite, aka RewriteEngine. http://httpd.apache.org/docs/2.2/misc/rewriteguide.html has some interesting examples for the latest version of Apache.
 
So I'm guessing here, but wordpress does something similar if you enable the readable page links. I.e. instead of http://www.mywordpress.com/index.php?p=124 you can make it do http://www.mywordpress.com/some_entry/2005/05/this_is_how_it_works

And wordpress figures it out under the hood somehow. It involves something being enabled on your server called mod_rewrite. Not sure if that helps.

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

You can usually enable mod_rewrite via a configuration file in your root directory if you pay for shared hosting.
 
Using content negotiation with Apache's multiviews is your solution.
 
You can try this in .htaccess (replacing the [ ] with the appropriate < and >.

[FilesMatch "."]
ForceType application/x-httpd-php
[/FilesMatch]


The period . is I think to match any file.

But I think if you configure it in httpd.conf file it is a lower server hit, but only if you have access to it.

Goodluck and warm regards
 
It's starting to seem that my Apache configuration doesn't like the application/x-httpd-php MIME type that is supposed to work for PHP. Whenever I specify it any of the ways you kind folks have suggested, I either get a Server Error as when I first tried with DefaultType, or browsers want to download the file as an application instead of displaying it.

So I've hacked around the problem. One email correspondent suggested writing a .php template file that includes the HTML from each file I want with a script, which is a cool solution, but that's not what I did. And I'm using Blogger, not WordPress, so those options aren't viable here either.

Rather, I just went back to Blogger's good conditional template tags for my individual post pages. It means that to make changes I have to edit my master template instead of the include files for those pages, which is a bit of extra work, but it also made it easy to change the sidebar and footer for those pages as well, so it works out.

I'm still puzzled about it though, because all the documentation I've read indicates that specifying application/x-httpd-php should work, but for me it doesn't.
 
Try doing it with the file relative path instead of the full url. PHP is probably configured to deny remote url open includes (security precaution)

change include 'http://www.domain.com/sidebar.php'

to include('/..file or root/relative/path/to/sidebar.php')
 
DefaultType application/x-httpd-php

- that works perfectly well for me, i don't know why that doesn't work for you