Forum Moderators: phranque

Message Too Old, No Replies

.htaccess (enable php in html, .txt file SSI)

htaccess config for SSI and PHP

         

dickey

1:43 pm on Sep 4, 2009 (gmt 0)

10+ Year Member



Hello - My first post to this forum.

Consider the following code below:

<html>
<body>
Some text should appear...
<p>
<!--#include virtual="sometext.txt"-->
<?php include('sometext.txt');?>
</p>
</body>
</html>

I have three objectives:

1. to run php.
2. to run php within .html and .htm files.
3. to have .txt files included as SSI execute.

1 & 2 resolve by adding:
AddType application/x-httpd-php5 .php .html
to my .htaccess file. Works fine and executes PHP for .php and .html files, but the .txt SSI don't display.

3. I am not so sure about. Have tried a myriad of variations the least of which is
AddHandler server-parsed .txt which does display the .txt file include.

I know a workaround is to change <!--#include virtual="sometext.txt"--> to <?php include('sometext.txt');?>, however I would like both implementations to work.

Any assistance, greatly appreciated.

Kind regards, Andrew

jdMorgan

2:56 pm on Sep 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since both "SSI" and "PHP" are server-side includes, you're really limited to one or the other, since you have to tell the server which 'language' to parse the HTML for.

You best bet is to go with the PHP solution, as it is 20 years 'more modern.'

If you have some legacy files where you only want to include SSI and not PHP, then you have the option to enclose the "AddType" and/or "AddHandler" directives within a <FilesMatch> section.

Jim

Caterham

5:07 pm on Sep 4, 2009 (gmt 0)

10+ Year Member



Unless you're hosted on legacy servers, SSI is implemented as an output filter, not as a handler; hence there's no problem to run php's handler and the result through mod_include. Consult mod_include's manual.

dickey

12:40 am on Sep 5, 2009 (gmt 0)

10+ Year Member



Thanks jdMorgan and Caterham for your helpful replies.

To jdMorgan:

Since I have just upgraded (what I would describe as a legacy site) with php tags in every page of the site for visitor analysis, it is perhaps now easier to run a script to upgrade all the <!--#include virtual="sometext.txt"--> tags to <?php include('sometext.txt');?> tags for consistency.

To Caterham

Admittedly, beyond enabling php, I seldom mess with .htaccess.

Given our current implementation of Apache(2.2.13 (Unix)), I assume SSI is implemented as an output filter (as I read mod_include has been implemented as an output filter since Apache 2.0).

I read the Apache Module mod_include, and remain a little confused as to the order of implementation, and indeed syntax in the .htaccess file.

For example:

AddType application/x-httpd-php5 .php .html
Options +Includes
AddType text/plain .txt
AddOutputFilter INCLUDES .txt

doesn't work, perhaps you might offer a further lead in the right direction, otherwise I would fall back to the solution in my reply to jdMorgan above.

Kind regards, Andrew

jdMorgan

1:06 am on Sep 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



AddType declares a MIME-type. Look at AddHandler instead.

It's likely that you want the output of your php scripts served with a MIME-type (i.e. HTTP Content-Type header) of text/html, not "application/x-"

Jim