Apache自定义文件类型处理

Apache自定义文件类型处理

本文介绍了PHP/Apache自定义文件类型处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以为Apache创建由PHP文件处理的自定义文件类型处理.

I'm wondering if it's possible to create custom file type handling for Apache that would be handled by a PHP file.

我要完成的工作是创建一种自定义的 .phps 扩展名,以在浏览器中查看代码.

What I'm trying to accomplish, is to create a sort of custom .phps extension for reviewing code in the browser.

我正在使用CodeMirror来显示代码,但是我希望此过程是自动化的,因为我只是将 .php 扩展名更改为 .phps ,其他一些 .php 文件将处理该 .phps 文件,因此我可以将该 .phps 文件的源代码注入HTML文件中使用CodeMirror.

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.

澄清一下-我想将此规则设置为从apache.conf使用,这意味着它可以在所有虚拟主机等上运行,而不是从 .htaccess 文件开始运行,而该文件必须是在服务器上的每个目录中.

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.

我认为这应该与apache.conf中的 FilesMatch 一起使用,现在这就是我要修改的内容:

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>

但是我收到一个 400 Bad Request 错误.我想在这样的基础上进行构建,以便Apache可以从其配置中处理 .phps 文件,而不必在每个配置中都使用 .htaccess 文件服务器上的目录.

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.

推荐答案

好,我知道了,这是我所做的:

OK I figured this out, here's what I did:

apache.conf 中,我添加了以下内容:

In apache.conf I added this:

<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php
    RewriteEngine On
    RewriteRule ^(.*)$ /tools/code/index.php [QSA,L]
</FilesMatch>

/tools/目录被添加为别名

Alias /tools "C:/Web/httpdocs/tools/"

将定义的 index.php 文件用作处理程序"就足够了.

That's enough for using the defined index.php file as a "handler".

我使用了 Ace编辑器来显示代码.要加载 .phps 文件以在编辑器中查看,我在编辑器文件中添加了一个不可见的< textarea> 元素,并在其中加载了源代码:

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>

瞧,我有一种查看 .phps 文件的更高级的方法.感谢您帮助我步入正轨.

And voila, I have a more advanced way of viewing .phps files. Thanks for helping me get on track.

这篇关于PHP/Apache自定义文件类型处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:53