【问题标题】:Beginner / How to configure my .htaccess file so that it runs the PHP inside my HTML files?初学者/如何配置我的 .htaccess 文件,以便它在我的 HTML 文件中运行 PHP?
【发布时间】:2017-11-07 23:15:44
【问题描述】:

所以我正在尝试配置我的.htaccess 文件,以便它可以在 HTML 文件中运行 PHP 代码。

当我转到mysite.com/thefile.php 时,它可以正常运行,没有任何问题。如果我去mysite.com/thefile.html 也是一样。但是如果我在 HTML 文件中添加<?php echo "Hello"; ?>,它不会显示任何内容。实际上,当我右键单击“查看源代码”时,它会显示 php 行。

这是我对 .htaccess 文件的尝试:

RemoveHandler .html .htm

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

我也试过了

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

但它显示“500 Internal Server Error”,上面写着:

The server encountered an internal error or misconfiguration and was unable to complete your request. 

The most common causes of this problem are errors in the app's .htaccess file or incorrect file or directory permissions preventing the web server from reading the .htaccess file. 

More information about this error is available in the app's Apache error log at:

/srv/users/SYSUSER/log/APPNAME/APPNAME_apache.error.log

总的来说,我对 Web 开发还很陌生,我想提一下,除了 .htaccess 文件中的这 2/1 行之外,我没有别的东西。也许我错过了一些行?只是猜测

谢谢大家(如果有的话,谢谢你们!)

【问题讨论】:

  • 别这样!你想完成什么?
  • 什么意思?我只是想在我的 HTML 文件中显示来自我的数据库的信息
  • 从您的 PHP 文件中执行。
  • 哪个版本的 apache 和哪个风味/版本的 linux?
  • 你需要的是 AddHandler,而不是 AddType。

标签: php html apache .htaccess


【解决方案1】:

使用 Ubuntu/Apache 2.4.x,在您的 /etc/apache2/mods-enabled/php7.1.conf 或等效项中,您可以添加:

<FilesMatch ".+\.html$"> SetHandler application/x-httpd-php </FilesMatch>

(但你根本不应该这样做!为 PHP 代码使用 .php 文件。)

【讨论】:

  • 在 .html 文件中执行 PHP 有什么不好?不争论,只是好奇你的理由。
  • 已经有一个专用于 PHP 的扩展。为什么要污染另一个文件扩展名并通过 PHP 解释器强制每个静态内容?当您的站点增长并且您将其移动到新服务器并忘记添加该配置时会发生什么?现在,您的所有 PHP 代码(包括数据库用户名和密码)都将公开,直到您修复它为止。如果您进行 apache 升级并且不小心擦除了该配置并且必须找到备份进行恢复,会发生什么情况?一切都暂时暴露了。
猜你喜欢
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 2022-06-22
  • 2018-08-12
相关资源
最近更新 更多