使用自訂 .htaccess 檔案的虛擬主機上出現錯誤 404
P粉729198207
P粉729198207 2023-09-05 10:28:49
0
2
618
<p>我在本機 Linux 伺服器上安裝了 apache2。它有一個名為 <code>pcts.local</code> 的虛擬主機,其根目錄為 <code>/var/www/repos/pcts/</code>。 pcts.local 的根目錄內是一個 .htaccess 文件,如果未如下給出,該文件會嘗試重寫 url 以包含 .php:</p> <pre class="brush:php;toolbar:false;">http://pcts.local/ -> http://pcts.local/index.php http://pcts.local/contact -> http://pcts.local/contact.php</pre> <p>問題是,<code>http://pcts.local/contact</code> 給出錯誤404,但<code>http://pcts.local/contact.php</code> 給出200。 </p> <h3>虛擬主機配置:</h3> <pre class="brush:php;toolbar:false;"><VirtualHost *:80> ServerName pcts.local ServerAdmin webmaster@localhost DocumentRoot /var/www/repos/pcts ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost></pre> <h3>.htaccess 檔案在 <code>/var/www/repos/pcts/</code></h3> <pre class="brush:php;toolbar:false;">RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(. )$ $1.php [NC,L]</pre> <p>提前感謝您的幫忙! </p>
P粉729198207
P粉729198207

全部回覆(2)
P粉151720173

在您的程式碼中,REQUEST_FILENAME 需要一個帶有 php 副檔名的檔案來執行重寫。

試試這個:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]
P粉009828788

如果這是您的完整配置,則您的 .htaccess 檔案不會被處理。

您尚未啟用特定目錄的 .htaccess 覆蓋。 (即,您尚未啟用 .htaccess 檔案的解析。)預設情況下,.htaccess 覆寫處於停用狀態。

但是,您也沒有啟用對檔案系統此區域的存取?您是否在伺服器配置的其他地方完成了此操作? !

您應該在 容器內有一個相關的 部分,如下:

<Directory /var/www/repos/pcts>
    # Enable .htaccess overrides
    AllowOverride All

    # Allow user access to this directory
    Require all granted
</Directory>

如果需要,您可以進一步限制 .htaccess 覆蓋(請參閱下面的參考連結)

參考:

#
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!