如何保存扩展名为.php或.html的PHP文件? [英] How can I save a PHP file with a .php or .html extension?

查看:142
本文介绍了如何保存扩展名为.php或.html的PHP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们有一个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.

您的Web服务器配置为通过查看扩展名来检测文件类型.默认情况下,它将通过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号;只需在其中添加描述即可使页面的Google排名得到提升.有关所涉及技术的完整讨论不在此问题的范围内,但是有很多资源可以为您提供帮助(尝试从此站点上的此处开始搜索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文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆