为什么我们有一个 .php 格式的 PHP 文件?我们可以将 PHP 文件保存为 .html,因为在某些网站上,我看到了带有 .php 扩展名的网页吗?为什么我们不能为 PHP 文件添加 .html 扩展名?
Why do we have a PHP file as .php? Can we save a PHP file as .html, because at some sites, I have seen webpages with the .php extension? Why can't we have a .html extension for a PHP file?
它会影响 PHP 文件在 PHP 服务器引擎上的执行吗?如果我们将其作为 .html 文件,其中包含 PHP 代码.
Does it effect the execution of the PHP file on the PHP server engine? If we have it as a .html file, which has PHP code inside it.
如果你真的想用 .html 文件来服务你的 PHP 代码,可以这样做.
If you really want to serve your PHP code with .html files, it can be done.
您的网络服务器配置为通过查看扩展名来检测文件类型.默认情况下,它将通过 PHP 解释器路由 .php 文件,而 .html 文件将直接提供给最终用户.您可以通过服务器的配置文件或单个 Web 目录的本地 .htaccess 文件来配置此行为.请参阅@kjy112 的回答了解如何执行此操作.
Your web server is configured to detect the file type by looking at the extension. By default it will route .php files through the PHP interpreter, and .html files will be served directly to the end user. You can configure this behaviour via the server's config file, or the local .htaccess file for the individual web directory. See @kjy112's answer for how to do this.
问题是为什么?这样做对你来说很重要,还是只是你想要的东西.问问自己:您的最终用户会关心吗?甚至注意到?
The question is why? Is it important for you to do this, or is it just something you want as a nicety. Ask yourself: Will your end-user care? or even notice?
默认情况下以这种方式配置浏览器的原因是,可以将没有任何 PHP 代码的纯 HTML 文件提供给用户,而无需经过 PHP 解释器处理的开销.通过更改配置,您的所有 HTML 文件都将被解释为 PHP,即使它们实际上不包含任何 PHP 代码,因此您将向服务器添加额外的处理负载.当然,如果您的所有文件都包含一些 PHP 代码元素,那么无论如何您都会想要这种行为,所以这显然不会让您担心.
The reason the browser is configured this way by default is so that plain HTML files that don't have any PHP code can be served to the user without going through the overhead of being processed by the PHP interpreter. By changing the configuration, all your HTML files will be interpreted as PHP even if they don't actually contain any PHP code, so you'll be adding extra processing load to your server. Of course, if all your files have some element of PHP code, you're going to want this behaviour anyway, so this obviously wouldn't worry you.
这样就可以了.但可能有更适合您的解决方案:
So it can be done. But there may be a better solution for you:
目前 SEO(搜索引擎优化)的趋势是完全摆脱 URL 中的文件扩展名.如果您查看此问题的 URL,您会发现它有一个非常具有描述性的 URL,并且没有扩展名.如果您想遵循最佳实践的当前趋势,您可能需要考虑沿此路线走.
The current trend for SEO (search engine optimisation) is to get rid of file extensions in the URL entirely. If you look at the URL for this question, you'll see that it has a very descriptive URL, and without an extension. If you want to follow current trends of best-practice, you may want to consider going down this route instead.
这是使用 mod_rewrite
完成的.URL 的描述部分是完全任意的:如果你改变它,页面仍然会正确加载.重要的部分是描述前的ID号;描述只是为了提高页面的谷歌排名.对所涉及技术的完整讨论超出了此问题的范围,但有大量资源可以帮助您(尝试在此站点上搜索 mod_rewrite 开始).
This is done using mod_rewrite
. The descriptive part of the URL is completely arbitrary: if you change it, the page will still load correctly. The important part is the ID number before the description; the description is simply there to give the page a boost with it's Google ranking. A full discussion of the techniques involved is outside the scope of this question, but there are plenty of resources around that will help you (try searching for mod_rewrite right here on this site to start with).
注意:我在这里给出的所有特定服务器配置建议都适用于 Apache Web 服务器.如果您使用的是 IIS 或其他一些服务器产品,您可能需要相应地对其进行调整.
这篇关于如何保存扩展名为 .php 或 .html 的 PHP 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!