Forum Moderators: phranque

Message Too Old, No Replies

Forcing non-labeled files to PHP

.htaccess should work, but how?

         

jonknee

7:35 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



I have a site with URLs in this form:

http://www.site.com/category/foo

and

http://www.site.com/year/month/day/keyword_or_title

It works great. I have this in my .htaccess file so that the files without an extension get shoved out as HTML:

DefaultType text/html

Now, how do I send these files to PHP? I can't get it with the conventional AddHandler because there aren't any file extensions to add.

Thanks!

jdMorgan

8:24 pm on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jonkee,

If I understand what you're trying to accomplish, something like this might work:


Options +FollowSymLinks
RewriteEngine on
# If not a directory and no file extension, rewrite to php script
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME}/!-d
RewriteRule !\. /your_php_script.php [L]

Be very sure that your script returns proper server response codes for File Not Found (404), Gone (410), Forbidden (403), and all other errors. Also, make sure you do not unconditionally return a 200-OK for each and every possible URL. If you do, the search engine spiders will consider your site a spider trap, and will limit the depth to which they spider. Your script needs to make a decision about whether is has specific content to return for each requested URI, and if not, return a 404 or 410 response.

You do not need to set the MIME type to php; The MIME-type returned to the client browser should reflect the type of *content* it returns, i.e. usually text/html, not the type of script your are using.

Ref: Apache mod_rewrite documentation [httpd.apache.org]

Jim

jonknee

10:17 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Not quite what I'm after, but close. The files themselves don't have an extension (foo not foo.php). I want PHP to execute the file as long as it exists. Right now it's just being pumped out as text/html.

All the errors fire as normal.

jdMorgan

10:34 pm on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you mean that you want each file parsed for included php code?

Jim

jonknee

10:53 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Yes. I want each file that doesn't have an extension (my pages) to be executed by PHP. I have a simple include I need to run.

Example directory:

  • foo.jpg
  • bar.gif
  • page_1.html
  • page_2.txt
  • page_3
  • page_4

Of that listing, I want page_3 and page_4 to be executed by PHP.

olwen

11:04 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



In my .htaccess I have:

<Files filename>
ForceType application/x-httpd-php
</Files>

which processes one file as php.
maybe you need:

DefaultType application/x-httpd-php

jonknee

11:07 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Olwen, that worked! Thanks so much. I'll do a little more digging, but it looks like it's a great solution.

Longhaired Genius

11:34 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Option MultiViews might be worth looking at, too.