将 HTML 解析为 PHP [英] Parse HTML as PHP
问题描述
如果我们将 Apache Web 服务器设置为将 Apache 配置为将所有 HTML 处理为 PHP,是否存在任何安全/性能问题?我特别指的是:
Are there any security / performance concerns if we set the Apache web server to configure Apache to handle all HTML as PHP? I was specifically referring to:
AddType application/x-httpd-php .php .php3 .php4 .html
我曾经遇到过需要将一些 PHP 逻辑添加到一些 HTML 文件中的情况;理想情况下,我不必更改文件名,例如page.html
到 page.php
(保持 page.html
的页面排名等).
I was in a situation where I needed to add some PHP logic into some HTML files; ideally, I didn't have to change the filename e.g. page.html
to page.php
(to keep the page rank, etc. for page.html
).
这与以下问题有关:httpd AddType 指令
从下面现有的答案/评论来看,社区似乎建议使用重定向或仅针对特定的 HTML 文件.限制是我正在重新设计一个现有站点(400 多个 HTML 页面;每个页面都使用某种 Dreamweaver 模板,从不同的文件中提取页眉和页脚).我希望完全避免 Dreamweaver 转向非专有的东西.所以,我有两个选择:
Edits: From the existing answers / comments below, it looks like the community suggests to either use redirects or only target specific HTML files. The constraint is that I am redesigning an existing site (400+ HTML pages; each of them uses some sort of Dreamweaver template that pulls in the header and footer from different files). I was hoping to completely shy away from Dreamweaver move into something non-proprietary. So, I am down with two options:
- 使用服务器端包含 (SSI) 来拉入页眉和页脚.这将导致我的所有 HTML 文件都使用 SSI 进行修饰.
- 添加一些 PHP 代码片段以包含页眉和页脚.对于这个选择,我必须确保文件名保持不变.
- Use Server Side Includes (SSI) to pull in the header and footer. This will result in all my HTML files to be decorated with SSI.
- Sprinkle some PHP snippet to include the header and footer. For this choice, I have to make sure the file name stays unchanged.
推荐答案
服务器确定它需要通过 PHP 解释器传递的文件越多,所涉及的开销就越大,但我认为这是不言而喻的.如果您的网站没有任何带有纯 HTML 的页面,那么您已经付出了可能付出的所有性能损失 - 将 HTML 添加到列表与在这种情况下与简单地重命名所有页面没有什么不同具有 .php 扩展名的文件.
The more files the server determines it needs to pass through the PHP interpreter, the more overhead involved, but I think this goes without saying. If your site does not have ANY pages with plain HTML, then you're already paying all the performance penalties that you could possibly pay - adding HTML to the list is no different in this case than simply renaming all the files to have a .php extension.
如果您确实拥有纯 HTML 页面,那么真正的性能损失将会出现 - 服务器将不必要地将这些页面传递给 PHP 以在不需要时进行解释.但即便如此,它也不是戏剧性的 - 那些 HTML 页面不需要 PHP 解释器,所以除了确定它不需要做任何事情之外,它不会做任何事情.这有成本,但并不重要.
The real performance penalty would come if you do have plain HTML pages - the server will needlessly pass these pages to PHP for interpretation when none is necessary. But even then, it isn't dramatic - the PHP interpreter won't be needed for those HTML pages, so it won't do anything aside from determining that it doesn't need to do anything. This has a cost, but it isn't significant.
现在,如果我们在这里谈论高容量,那么每一点性能都很重要,这将不是一个可行的解决方案.但是,对于中低容量网站,性能损失为零.
Now, if we're talking high-volume here, every little bit of performance matters and this would not be a practicable solution. For low- to mid-volume sites, however, the performance penalty would be nill.
如果这是一次性更改并且受影响的文件数量有限,那么使用 FilesMatch
指令可能更保守.
If this is a one-time change and there are a limited number of files that are affected, then it may be more conservative to use a FilesMatch
directive.
<FilesMatch "^(file_one|file_two|file_three)\.html$">
AddType application/x-httpd-php .html
</FilesMatch>
这篇关于将 HTML 解析为 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!