【问题标题】:Run php on .html pages with .htaccess使用 .htaccess 在 .html 页面上运行 php
【发布时间】:2021-01-11 14:31:23
【问题描述】:

想要使用 DDEV 进行本地开发,并且我有一个包含遗留内容的站点,需要在扩展名为 .html 的页面上运行 php。在其他测试环境中,我在 .htaccess 中使用了不同的行来启用它。使用我的 ddev 测试环境,我还不知道什么是有效的。

更新 以下是我在 .htaccess 中尝试过的行(单独或组合取消注释):

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

#AddType application/x-httpd-php7 .html .htm
#AddHandler application/x-httpd-php7 .htm .html
    
#AddHandler x-httpd-php .htm .html
#AddType x-httpd-php .html .htm
    
#AddType x-httpd-php7 .html .htm
#AddHandler x-httpd-php7 .htm .html

#AddType x-httpd-php73 .htm .html
#AddHandler x-httpd-php73.htm .html
#AddHandler x-httpd-php7-3 .htm .html
      
#AddType application/x-httpd-php73 .html .htm
#AddType application/x-httpd-php7-3 .html .htm
#AddHandler application/x-httpd-php7-3 .htm .html

我还尝试使用以下内容更新 .ddev/apache/apache-site.conf:

<FilesMatch ".html$"> 
SetHandler "proxy:fcgi://php:9000" 
</FilesMatch> 

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

但我承认这不是我熟悉的东西,所以可能没有完全正确。

macOS Big Sur, ddev1.16.5 with php7.3, ddev webserver_type: apache-fpm.

有什么建议吗?谢谢!

【问题讨论】:

  • 你没有提到你是否在 ddev 中使用webserver_type: apache-fpm。默认是 nginx-fpm,它不会尊重你的 .htaccess...
  • 谢谢@rfay - 我设置了 webserver_type: apache-fpm,很好。但是,问题仍然存在......我试过了:`#AddType application/x-httpd-php .html .htm #AddHandler application/x-httpd-php .htm .html #AddType application/x-httpd-php7 .html .htm #AddHandler application/x-httpd-php7 .htm .html AddHandler x-httpd-php .htm .html AddType x-httpd-php .html .htm #AddType x-httpd-php7 .html .htm #AddHandler x-httpd-php7 .htm .html #AddType x-httpd-php73 .htm .html #AddHandler x-httpd-php73.htm .html `
  • 我还尝试使用以下内容更新 .ddev/apache/apache-site.conf: SetHandler "proxy:fcgi://php:${APACHE_FCGI_HOST_PORT}" SetHandler applicaiton/x-httpd-php 但不可否认,这不是我熟悉的东西,所以可能不太正确?此外,apache-site.conf 被 ddev stop/start 覆盖,因此在编辑 .conf 后使用“ddev exec apachectl -k graceful”进行测试,但应该能够保留这些更改吗?再次感谢!
  • 哈哈我看到了允许site.conf的自定义保留的行!很明显,我还在适应……

标签: php .htaccess ddev


【解决方案1】:

问题在于 php-fpm 未配置为允许使用 .html 文件扩展名。这是在 /etc/php//fpm/pool.d/www.conf 中配置的。

因此,您需要做两件事来启用您所追求的行为,即允许 PHP 解释 HTML 文件。

  1. 允许在 pool.d/www.conf 中的 php-fpm 配置中处理 html 文件。您可以使用https://stackoverflow.com/a/65693405/215713 中的答案来执行此操作
  2. 允许在 apache 配置中处理 html 文件。我确信有不止一种方法可以做到这一点,你也许可以在 .htaccess 中做到这一点,但我通过在 .ddev/apache/apache-site.conf 中添加这节端口 80 虚拟主机来做到这一点(当然还有删除文件顶部的#ddev-generated):
 <FilesMatch ".+\.(html|ph(ar|p|tml))$">
      SetHandler "proxy:unix:/var/run/php-fpm.sock|fcgi://localhost"
  </FilesMatch>

【讨论】:

  • 谢谢@rfay,它有效!花了一段时间才回到这一点,但现在很高兴能切换到 ddev!
猜你喜欢
  • 2011-06-08
  • 2021-05-26
  • 2018-07-20
  • 2014-04-17
  • 2016-11-23
相关资源
最近更新 更多