简体   繁体   English

PHP / Apache自定义文件类型处理

[英]PHP/Apache custom file type handling

I'm wondering if it's possible to create custom file type handling for Apache that would be handled by a PHP file. 我想知道是否可以为Apache创建由PHP文件处理的自定义文件类型处理。

What I'm trying to accomplish, is to create a sort of custom .phps extension for reviewing code in the browser. 我要完成的工作是创建一种自定义.phps扩展名,以在浏览器中查看代码。

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. 我正在使用CodeMirror来显示代码,但我希望此过程是自动化的,因为我只是将.php扩展名更改为.phps ,而其他一些.php文件将处理该.phps文件,因此我可以注入的所述源代码.phps文件与CodeMirror一个HTML文件。

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. 需要澄清的是-我想将此规则设置为从apache.conf运行,这意味着它将在所有虚拟主机等上运行,而不是在服务器上每个目录中都需要的.htaccess文件中运行。

I'm thinking this should work with FilesMatch within apache.conf, right now this is what I'm tinkering with: 我认为这应该与apache.conf中的FilesMatch一起使用,现在这就是我要修改的内容:

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

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. 很好地解释了文件类型处理程序的工作方式,请看一下AddType命令。 However it is questionable if that is a good approach to what you describe as your goal, to visualize php source code. 但是,这是否是实现目标的可视化php源代码的好方法,这值得怀疑。 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: 例如,您可以使用以下URL并在内部对其进行重写:

/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: 这可以通过apache重写模块或在nginx中使用常规处理程序脚本来完成:

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. 这样,您不必定义某些本地文件类型,而可以命名php文件以使其按实际显示的名称:php文件。 I mean a php file is a php file. 我的意思是一个php文件是一个php文件。 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. 此外,它还允许对要重写的URL进行更细粒度的控制,并在需要时可以指定一些其他参数。


Two notes for the above rewrite rule: 上述重写规则的两个注意事项:

  • you may have to enable Apaches AllowEncodedSlashes to on , depending on your existing server setup 您可能必须启用Apaches AllowEncodedSlasheson ,具体取决于您现有的服务器设置
  • you have to change the syntax slightly if you want to use the rewriting rule inside .htaccess style files (you have to remove the leading slashes, depending on where you place that file). 如果要在.htaccess样式文件中使用重写规则,则必须稍稍更改语法(必须删除前导斜线,具体取决于放置该文件的位置)。 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 httpd.confvhost.conf添加以下代码,然后重新启动Apache服务器

<IfModule mime_module>
    AddType application/x-httpd-php .phps
</IfModule>

Or else you can use mod_rewrite 否则你可以使用mod_rewrite

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

OK I figured this out, here's what I did: 确定,我明白了,这是我做的:

In apache.conf I added this: apache.conf我添加了以下内容:

<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 /tools/目录被添加为别名

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

That's enough for using the defined index.php file as a "handler". 这足以将定义的index.php文件用作“处理程序”。

I used Ace editor to display the code. 我使用Ace编辑器显示代码。 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: 为了加载.phps文件以在编辑器中查看,我在编辑器文件中添加了一个不可见的<textarea>元素,并在其中加载了源:

<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. 瞧,我有一种更高级的查看.phps文件的方式。 Thanks for helping me get on track. 感谢您帮助我步入正轨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM