.htaccess在php專案中不生效

.htaccess在php專案中不生效

我正在運行 Ubuntu 20.04,目前正在使用 php 建立一個 api。我添加了一個.htaccess文件,該文件應該會將任何 404 頁面重定向回我的 index.php。我在askubuntu 中問這個問題,因為我相信這可能是我的apache 配置的問題。我進入我的000-default文件/etc/apace2/sites-available並修改了:

<Directory /var/www/html>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

我還修改了我的apache2.conf內容/etc/apache2/apache2.conf,如下所示:

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<Directory /usr/share>
    AllowOverride All
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

#<Directory /srv/>
#   Options Indexes FollowSymLinks
#   AllowOverride None
#   Require all granted
#</Directory>

這是我的 .htaccess 檔案的內容:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

</IfModule>

我嘗試編寫類似“THIS SHOULD ERROR”的內容,但是當我重新加載瀏覽器時,我沒有收到預期的 500 錯誤,但仍然出現 400 錯誤,所以這告訴我我的.htaccess文件沒有被命中。

修改任何 apache 或系統檔案後,我確實運行了

sudo systemctl restart apache2但沒有骰子。

目前不知道還需要修改什麼。如有任何幫助,我們將不勝感激,謝謝。

答案1

我懷疑問題不是您的 .htaccess 被忽略,而是 mod_rewrite 未啟用。嘗試sudo a2enmod rewrite並重新啟動 Apache2。

相關內容