PHP 文件未加载 .htaccess 文件

PHP 文件未加载 .htaccess 文件

我正在尝试弄清楚为什么在加载 .php 文件时没有考虑我的 .htaccess。以下文件位于我的本地主机的 webroot 中。

.htaccess

# Cause a HTTP 500
test

文件.html

<html><body><h1>This should not show</h1></body></html>

文件.php

<html><body><h1>This should not show</h1></body></html>

当我访问 /index.html 时,我得到了预期的 HTTP500 当我访问 /index.php 时,html 会显示。

知道为什么 .htaccess 无法加载 PHP 文件吗?

Apache 2.4.6 VHost(/etc/httpd/vhosts.d/website.local.conf):

    <VirtualHost *:443>
        ServerName website.local
        ServerAlias www.website.local

        DocumentRoot /var/www/vhosts/website/website
        <Directory /var/www/vhosts/website/website>
                require all granted
                Options Indexes FollowSymLinks
                AllowOverride All
        </Directory>


        # Trigger PHP-FPM to run PHP execution
        <IfModule proxy_fcgi_module>
            ProxyPassMatch "^/(.*\.php(/.*)?)$" "unix:/var/www/vhosts/website/php-fpm.sock|fcgi://website/var/www/vhosts/website/website"
            DirectoryIndex index.php
        </IfModule>

        SSLEngine on
        SSLCertificateKeyFile /var/www/ssl/website.key
        SSLCertificateFile /var/www/ssl/website.crt


</VirtualHost>

此站点没有其他 vhost 配置:

[root@localhost ~]# cat /etc/httpd/conf/*.conf | grep website.local
[root@localhost ~]# cat /etc/httpd/vhosts.d/*.conf | grep website.local
        ServerName website.local
        ServerAlias www.website.local
[root@localhost ~]#

更新 1:

我已按照 .htaccess 调试说明启用了 rewrite:trace3 日志级别https://stackoverflow.com/questions/5641618/how-to-enable-loglevel-debug-on-apache2-server。看起来 Apache 在加载 PHP 文件时甚至不考虑 .htaccess 文件:

访问“/file.html”-.HTAccess 已加载并按预期返回 HTTP500

==> /var/log/httpd/website-error_log <== 
[Thu Jul 07 09:36:02.651091 2016] [core:alert] [pid 2822] [client 10.128.3.189:56406] /var/www/vhosts/website/website/.htaccess: Invalid command 'test', perhaps misspelled or defined by a module not included in the server configuration

==> /var/log/httpd/website-access_log <==
10.128.3.189 - - [07/Jul/2016:09:36:02 +0100] "GET /wp-admin/ HTTP/1.1" 500 527 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"

访问“file.php”-.HTAccess 未加载并返回 HTTP200

==> /var/log/httpd/website-access_log <==
10.128.3.189 - - [07/Jul/2016:09:38:41 +0100] "GET /file.php HTTP/1.1" 200 64057 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
10.128.3.189 - - [07/Jul/2016:09:38:41 +0100] "GET /file.php?=PHPE9568F34-D428-11d2-A769-00AA001ACF42 HTTP/1.1" 200 2524 "https://www.website.local/file.php" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
10.128.3.189 - - [07/Jul/2016:09:38:41 +0100] "GET /file.php?=PHPE9568F35-D428-11d2-A769-00AA001ACF42 HTTP/1.1" 200 2146 "https://www.website.local/file.php" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"

访问“file.jpg”-.HTAccess 已加载并按预期返回 HTTP500

==> /var/log/httpd/website-error_log <==
[Thu Jul 07 09:43:08.403263 2016] [core:alert] [pid 2827] [client 10.128.3.189:56551] /var/www/vhosts/website/website/.htaccess: Invalid command 'sfdgsaga', perhaps misspelled or defined by a module not included in the server configuration

==> /var/log/httpd/website-access_log <==
10.128.3.189 - - [07/Jul/2016:09:43:08 +0100] "GET /file.jpg HTTP/1.1" 500 527 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"

我不知道有任何配置会禁止特定文件/mime 类型的 .htaccess。这可能是模块加载顺序的问题吗?

更新 2:清理上面的 vhost 文件

更新 3:仅当配置了 PHP-FPM 时才会出现问题从配置中删除以下代码不再跳过 .htaccess 文件

<IfModule proxy_fcgi_module>
ProxyPassMatch "^/(.*\.php(/.*)?)$" "unix:/var/www/vhosts/website/php-fpm.sock|fcgi://website/var/www/vhosts/website/website"
DirectoryIndex index.php
</IfModule>

更新 4:感谢@w3d 指出这一点。内部代理请求将跳过 .htaccess 文件。当然可以。我已将 VHost 文件更新为以下内容,并遵循此线程中的答案:Apache 2.4 + PHP-FPM + ProxyPassMatch

<VirtualHost *:443>
        ServerName website.local
        ServerAlias www.website.local

        DocumentRoot /var/www/vhosts/website/website
        <Directory /var/www/vhosts/website/website>
require all granted
                Options Indexes FollowSymLinks
                AllowOverride All



        </Directory>

        ErrorLog "logs/website-error_log"
        CustomLog "logs/website-access_log" combined env=!forwarded
        CustomLog "logs/website-access_log" proxy env=forwarded



        # Proxy set-up as per
        # https://serverfault.com/questions/450628/apache-2-4-php-fpm-proxypassmatch
        # This is to forward all PHP to php-fpm.
        <FilesMatch \.php$>
           SetHandler "proxy:unix:/var/www/vhosts/website/php-fpm.sock|fcgi://website/"
        </FilesMatch>

         # Set some proxy properties (the string "unique-domain-name-string" should match
         # the one set in the FilesMatch directive.
         <Proxy fcgi://website>
           ProxySet connectiontimeout=5 timeout=240
         </Proxy>
        DirectoryIndex /index.php index.php

        # If the php file doesn't exist, disable the proxy handler.
         # This will allow .htaccess rewrite rules to work and
         # the client will see the default 404 page of Apache
         RewriteCond %{REQUEST_FILENAME} \.php$
         RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
         RewriteRule (.*) - [H=text/html]

        SSLEngine on
        SSLCertificateKeyFile /var/www/ssl/website.key
        SSLCertificateFile /var/www/ssl/website.crt


</VirtualHost>

答案1

当您使用 https 访问该网站时,vhosts 配置的这一部分是相关的,您应该在 vhosts 中有另一个实例,您可以再查看并更新您的帖子吗?

答案2

感谢@w3d指出这一点。内部代理请求将跳过.htaccess文件。当然会跳过。我已将我的VHost文件更新为以下内容,并遵循此线程中的答案:Apache 2.4 + PHP-FPM + ProxyPassMatch

相关内容