简体   繁体   English

如何使用 .php 或 .html 扩展名保存 PHP 文件?

[英]How can I save a PHP file with a .php or .html extension?

Why do we have a PHP file as .php?为什么我们有一个 .php 格式的 PHP 文件? Can we save a PHP file as .html, because at some sites, I have seen webpages with the .php extension?我们可以将 PHP 文件保存为 .html,因为在某些站点上,我看到了带有 .php 扩展名的网页? Why can't we have a .html extension for a PHP file?为什么我们不能为 PHP 文件提供 .html 扩展名?

Does it effect the execution of the PHP file on the PHP server engine?它会影响 PHP 文件在 PHP 服务器引擎上的执行吗? 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.如果您真的想使用 .html 文件为您的 PHP 代码提供服务,这是可以做到的。

Your web server is configured to detect the file type by looking at the extension.您的 Web 服务器配置为通过查看扩展名来检测文件类型。 By default it will route .php files through the PHP interpreter, and .html files will be served directly to the end user.默认情况下,它将通过 PHP 解释器路由 .php 文件,而 .html 文件将直接提供给最终用户。 You can configure this behaviour via the server's config file, or the local .htaccess file for the individual web directory.您可以通过服务器的配置文件或单个 Web 目录的本地 .htaccess 文件配置此行为。 See @kjy112's answer for how to do this.有关如何执行此操作,请参阅 @kjy112 的答案。

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?甚至通知?

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.默认情况下以这种方式配置浏览器的原因是,可以将没有任何 PHP 代码的纯 HTML 文件提供给用户,而无需经历 PHP 解释器处理的开销。 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.通过更改配置,您的所有 HTML 文件都将被解释为 PHP,即使它们实际上不包含任何 PHP 代码,因此您将向服务器添加额外的处理负载。 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.当然,如果您的所有文件都包含一些 PHP 代码元素,那么无论如何您都会想要这种行为,所以这显然不会让您担心。

So it can be done.所以是可以做到的。 But there may be a better solution for you:但可能有更好的解决方案:

The current trend for SEO (search engine optimisation) is to get rid of file extensions in the URL entirely. SEO(搜索引擎优化)的当前趋势是完全摆脱 URL 中的文件扩展名。 If you look at the URL for this question, you'll see that it has a very descriptive URL, and without an extension.如果您查看此问题的 URL,您会发现它有一个非常具有描述性的 URL,并且没有扩展名。 If you want to follow current trends of best-practice, you may want to consider going down this route instead.如果您想遵循当前的最佳实践趋势,您可能需要考虑走这条路。

This is done using mod_rewrite .这是使用mod_rewrite完成的。 The descriptive part of the URL is completely arbitrary: if you change it, the page will still load correctly. URL 的描述部分是完全任意的:如果您更改它,页面仍将正确加载。 The important part is the ID number before the description;重要的部分是描述前的ID号; 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).对所涉及技术的全面讨论超出了本问题的范围,但有大量资源可以帮助您(尝试在本网站上搜索 mod_rewrite 开始)。

Note: All the specific server config advice I've given here is applicable to the Apache web server.注意:我在这里给出的所有特定服务器配置建议都适用于 Apache Web 服务器。 If you're using IIS or some other server product, you may need to adapt it accordingly.如果您正在使用 IIS 或其他一些服务器产品,您可能需要相应地调整它。

You can do it, but you have to modify your .htaccess file:你可以这样做,但你必须修改你的.htaccess文件:

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

Rewrite Info here在这里重写信息

all requests to file.htm will be sent to file.php :所有对file.htm 的请求都将发送到file.php

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [NC]

kjy 答案的替代方法是使用 URL 重写,它可以让您使用任何您想要的页面扩展名/文件名。

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

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