【发布时间】:2015-07-27 17:15:17
【问题描述】:
我的 SLIM PHP 应用程序位于 http://www.example.com/API/
我的index.php 中有路由,如下所示:
$app->get('/getstudents', function() use($app) {
$db = new DbHandler();
$result = $db->getAllStudents();
if ($result === false) {
echo "Failed to fetch";
} else {
echo json_encode($result);
}
});
$app->run();
当我尝试使用 curl http://example.com/API/getstudents 的 url 时,我收到错误 在此服务器上找不到请求的 URL /API/getstudents。
如果我使用curl http://example.com/API/index.php/getstudents,学生列表将按预期返回。我用谷歌搜索并了解到这可能是因为.htaccess 文件和/或虚拟主机设置。
所以我已将.htaccess 从API/vendor/slim/slim/.htaccess 复制到API 文件夹,因此它与index.php 位于同一文件夹中。在这个文件中我有:
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
RewriteEngine On
RewriteBase /API/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
这不走运。是否需要同时更改 API 目录和 API/vendor/slim/slim/ 中的.htaccess 文件的内容?
我也不完全确定我需要对我的虚拟主机文件做什么。在 /etc/apache2/sites-available 我正在编辑 example.com.conf,内容是:
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin webmaster@example.co
ServerName www.example.co
ServerAlias example.co
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/example.co/public_html
# Log file locations
LogLevel warn
ErrorLog /var/www/html/example.co/logs/error.log
CustomLog /var/www/html/example.co/logs/access.log combined
<Directory "/var/www/html/example.co/public_html">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
有人可以指点一下我在这里可能做错了什么吗?谢谢!
【问题讨论】:
-
你可以试试this .htaccess file吗?
标签: php apache .htaccess mod-rewrite slim