Forum Moderators: coopster
'type¦T=MIME-type' (force MIME type)
Force the MIME-type of the target file to be MIME-type. This can be used to set up the content-type based on some conditions. For example, the following snippet allows .php files to be displayed by mod_php if they are called with the .phps extension:RewriteRule ^(.+\.php)s$ $1 [T=application/x-httpd-php-source]
I played around with it for a few hours but after hitting a rough spot I never got around to having another look at this little side project. As I was doing some cleanup today I came across the code again and my notes. And of course I had to fiddle with it one more time but ended up empty again. I thought I would throw what I had out here to the community and see if anybody else has any ideas on the issue.
I have a directory of PHP scripts. Every entry in the directory has a .php extension. Often times I would like to view the source for the script through the browser as well. PHP has a nice little application handler for doing just that, simply save your script with a .phps extension and when clicked, the MIME type application handler will show highlighted PHP source (as long as you have enabled it in your configuration). OK, but I don't want to save each and every script twice, once with a .php extension and once with a .phps extension. I thought, let's turn to Apache and see what we can do.
I wrote a quick little PHP application that reads the directory and creates a table of the PHP scripts. The script itself appears in the first column and a link in the right column to the fake .phps source document. Looks something like this (source for script included below, put it in the same directory as your scripts):
+--------+---------+<?php
¦ script ¦ source ¦
+--------+---------+
¦ aa.php ¦ aa.phps ¦
¦ bb.php ¦ bb.phps ¦
+--------+---------+
Now, in a per-directory override file (.htaccess), I've thrown in the following...
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^.]+\.php)s$ $1 [T=application/x-httpd-php-source]
However, upon clicking the source link in the right column, the PHP parser processes the script rather than displaying the highlighted source code. I cranked up the RewriteLog to level 9 and here is the process ...
Often times I would like to view the source for the script through the browser as well.
I'm curious as to why you want to take this approach. Why wouldn't you want to make your life easier by using a function such as highlight_file [us2.php.net]? It essentially achieves the same thing.
But to answer your question, I have no idea, either. ;)
I wouldn't say the 'highlight_file' approach would be easier though as I would still need to code up a directory list, but then I would also need to
All that said, the problem, I believe, lies in the handler after the redirect. mod_rewrite states that ...
API PhasesFirst you have to understand that when Apache processes a HTTP request it does this in phases. A hook for each of these phases is provided by the Apache API. Mod_rewrite uses two of these hooks: the URL-to-filename translation hook which is used after the HTTP request has been read but before any authorization starts and the Fixup hook which is triggered after the authorization phases and after the per-directory config files (.htaccess) have been read, but before the content handler is activated.
I find that it works like a dream once I add a 'php' extension to the source handler ...
AddType application/x-httpd-php-source phps php... but therein lies the catch! How do you process the files and see their source code at the same time? I have an idea, am going to test. Not until tomorrow though, been a long weekend ...