如何使用 .php 或 .html 扩展名保存 PHP 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5155256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I save a PHP file with a .php or .html extension?
提问by parmanand
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 文件?我们可以将 PHP 文件保存为 .html,因为在某些站点上,我看到了带有 .php 扩展名的网页?为什么我们不能为 PHP 文件提供 .html 扩展名?
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.
它会影响 PHP 文件在 PHP 服务器引擎上的执行吗?如果我们将其作为 .html 文件,其中包含 PHP 代码。
回答by Spudley
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. 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.
您的 Web 服务器配置为通过查看扩展名来检测文件类型。默认情况下,它将通过 PHP 解释器路由 .php 文件,而 .html 文件将直接提供给最终用户。您可以通过服务器的配置文件或单个 Web 目录的本地 .htaccess 文件配置此行为。有关如何执行此操作,请参阅 @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. 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.
默认情况下以这种方式配置浏览器的原因是,可以将没有任何 PHP 代码的纯 HTML 文件提供给用户,而无需经过 PHP 解释器处理的开销。通过更改配置,您的所有 HTML 文件都将被解释为 PHP,即使它们实际上不包含任何 PHP 代码,因此您将向服务器添加额外的处理负载。当然,如果您的所有文件都包含一些 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. 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.
SEO(搜索引擎优化)的当前趋势是完全摆脱 URL 中的文件扩展名。如果您查看此问题的 URL,您会发现它有一个非常具有描述性的 URL,并且没有扩展名。如果您想遵循当前的最佳实践趋势,您可能需要考虑走这条路。
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).
这是使用mod_rewrite
. URL 的描述部分完全是任意的:如果你改变它,页面仍然可以正确加载。重要的部分是描述前的ID号;描述只是为了提升页面的谷歌排名。对所涉及技术的全面讨论超出了本问题的范围,但有大量资源可以帮助您(尝试在本网站上搜索 mod_rewrite 开始)。
Note: All the specific server config advice I've given here is applicable to the Apache web server. If you're using IIS or some other server product, you may need to adapt it accordingly.
注意:我在这里给出的所有特定服务器配置建议都适用于 Apache Web 服务器。如果您使用 IIS 或其他一些服务器产品,您可能需要相应地调整它。
回答by u476945
回答by ejohansson
all requests to file.htmwill be sent to file.php:
所有对file.htm 的请求都将发送到file.php:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ .php [NC]
回答by Tom Gullen
An alternative to kjy's answer is to use URL rewriting, it will let you use whatever page extensions/file names you want.
kjy 答案的替代方法是使用 URL 重写,它可以让您使用任何您想要的页面扩展名/文件名。