I'm wondering if it's possible to create custom file type handling for Apache that would be handled by a PHP file.
What I'm trying to accomplish, is to create a sort of custom .phps
extension for reviewing code in the browser.
I'm using CodeMirror to display the code, but I'd like this process to be automated, as in I just change the .php
extension to .phps
, and some other .php
file would handle that .phps
file so I could inject the source code of that .phps
file into a HTML file with CodeMirror.
I hope my goal is clear, I'm just wondering if it's at all possible.
Edit:
To clarify - I want to setup this rule to work from apache.conf, meaning that it would work across all the virtual hosts etc, not from a .htaccess
file that would be required to be in every directory on the server.
I'm thinking this should work with FilesMatch
within apache.conf, right now this is what I'm tinkering with:
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php
RewriteEngine on
RewriteRule ^(.*)$ /tools/code/?file=$1 [L]
</FilesMatch>
However I'm getting a 400 Bad Request
error. I'd like to build on top of something like this, so that Apache would handle .phps
files from within it's configuration, and not have to use .htaccess
files within every directory on the server.
This certainly is possible, just take a look at the documentation. The way file type handlers work is well explained, take a look at the AddType
command. However it is questionable if that is a good approach to what you describe as your goal, to visualize php source code. Wouldn't it be much easier and more elegant to use rewriting to hand over the requested file to a handler script which can "publish" it as you like?
For example you could use URLs like the following and internally rewrite it:
/source/path/to/file.php => /path/to/handler.php?source=%2Fpath%2Fto%2Ffile.php
This could be done with apache rewriting module or in nginx using a general handler script:
RewriteEngine on
RewriteRule ^/source/(.)*$ /path/to/handler.php?source=$1 [L,B]
That way you don't have to define some local file type and can name php files to be shown just as what they are: php files. I mean a php file is a php file. The file name extension should reflect type of what is contained in the file, not how it should be handled. That would mean you mingle separate layers of logic.
That is much more transparent to users in my view. Also it allows more fine grained control about what URLs to rewrite and maybe some additional arguments to specify, where desired.
Two notes for the above rewrite rule:
AllowEncodedSlashes
to on
, depending on your existing server setup .htaccess
style files (you have to remove the leading slashes, depending on where you place that file). But in general such files should be avoided whenever possible: they are notoriously error prone, hard to debug, sometimes cause unexpected side effects and really slow the server down. So if you have access to the servers host configuration you should prefer to place such rules in there. Add bellow code in httpd.conf
or vhost.conf
and restart Apache server
<IfModule mime_module>
AddType application/x-httpd-php .phps
</IfModule>
Or else you can use mod_rewrite
OK I figured this out, here's what I did:
In apache.conf
I added this:
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php
RewriteEngine On
RewriteRule ^(.*)$ /tools/code/index.php [QSA,L]
</FilesMatch>
the /tools/
directory is added as an alias
Alias /tools "C:/Web/httpdocs/tools/"
That's enough for using the defined index.php
file as a "handler".
I used Ace editor to display the code. To load the .phps
file for viewing within the editor, I added an invisible <textarea>
element to the editor file with the source loaded into it:
<textarea id="code" style="display:none;">
<?=file_get_contents(ltrim($_SERVER['DOCUMENT_ROOT'], '/').$_SERVER['REQUEST_URI']);?>
</textarea>
I modified the initialization code to suit my needs:
<script>
var editor = ace.edit('editor');
editor.session.setMode('ace/mode/php');
editor.setTheme('ace/theme/monokai');
editor.setValue(document.getElementById('code').value);
editor.gotoLine(1);
editor.setShowPrintMargin(false);
editor.setReadOnly(true);
</script>
And voila, I have a more advanced way of viewing .phps
files. Thanks for helping me get on track.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.