Php

未為 PHP 文件載入 .htaccess 文件

  • July 23, 2016

我試圖弄清楚為什麼在載入 .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>

此站點沒有其他虛擬主機配置:

[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:

我按照https://stackoverflow.com/questions/5641618/how-to-enable-loglevel-debug-on-apache2-server中的 .htaccess 調試說明啟用了 rewrite:trace3 loglevel 。看起來 .htaccess 文件在載入 PHP 文件時甚至沒有被 Apache 考慮:

訪問“/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"

我不知道有任何配置會禁止 .htaccess 用於特定文件/mime 類型。這可能是模組載入順序的問題嗎?

**更新 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>

感謝@w3d 指出這一點。內部代理請求將跳過 .htaccess 文件。當然可以。根據該執行緒的答案,我已將我的 VHost 文件更新為以下內容:Apache 2.4 + PHP-FPM + ProxyPassMatch

當您使用 https 訪問該網站時,vhosts 配置的這一部分是相關的,您應該在 vhosts 中有另一個實例,您可以再查看並更新您的文章嗎?

引用自:https://serverfault.com/questions/788231