【发布时间】:2019-02-15 22:53:18
【问题描述】:
我在使用 .htaccess 文件的超薄应用程序中重定向到 index.php 时遇到问题。 如果我在 URL 的末尾添加 index.php,路由就可以工作
所以slimapp.dev/hello/myname 出错了
Not Found The requested URL /hello/myname was not found on this server.
slimapp.dev/index.php/hello/myname 工作时
这是我的 .htaccess 文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php [QSA,L]
我在 Ubuntu 18.04 上使用 Apache 2
文件结构
public_html
|_index.php
|_ vendor
|_.htaccess
virtualHost 000-default.conf
<VirtualHost *:80>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
如果我使用php -S localhost:3000,它会按预期工作,但如果我使用 Apache Web 服务器,我最常将 index.php 添加到 URL 的末尾以使其正常工作。谢谢
【问题讨论】:
-
健全性检查:在您的 Apache 设置中,您将 Document Root 指向哪里?
-
另外,您确定您的虚拟主机的
AllowOverride设置正确吗?只是为了确定,你能发布你的虚拟主机块吗? -
除了@maiorano84 所说的之外,您的.htaccess 是否像示例中那样在克拉(^)和“index.php”之间有空格?如果是这样,您可以尝试删除空格。
-
@Hayden 这很好。额外的空间很可能会导致 OP 看到的问题。
-
在 Apache 中启用重写
a2enmod rewrite。