【发布时间】:2018-05-23 14:50:20
【问题描述】:
当我将 Codeigniter 应用程序放入子文件夹时,我无法访问我的 REST api 方法。我已将 Codeigniter 应用程序放在 rest 文件夹中。然后我尝试拨打http://myapp.dev/rest/api/Workingday/workingDay,我得到404 not found,如果我尝试访问http://myapp.dev/rest/index.php,也会出现同样的问题
这是我的文件夹结构
这是我的扩展 rest api 文件夹,所以你可以看到我在控制器中有 api 文件夹,并且会有公共和休息 API。
这是我的 .htaccess
RewriteEngine on
#RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /rest/index.php/$1 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
这是我在 Apache 服务器上的虚拟主机(本地主机开发) 名称VirtualHost *:80
<VirtualHost *:80>
ServerName myapp.dev
ServerAlias *.myapp.dev
ServerAdmin info@myapp.dev
DocumentRoot "/Users/user/workspace/myapp" LogLevel debug
ErrorLog "/Users/user/workspace/myapp/rest/application/logs/myapp.dev-error_log"
CustomLog "/Users/user/workspace/myapp/rest/application/logs/myapp.dev-access_log" common
</VirtualHost>
这是我的WorkingDay api,以防您在这里看到任何问题,也许......文件的位置是/Users//workspace/myapp/rest/application/controllers/api/Workingday.php
class Workingday extends REST_Controller {
public function workingDays_get($id=NULL){
if($id != NULL){
$condition['id_working_day'] = $id;
$workingDays = $this->Working_days->get($condition);
}else{
$workingDays = $this->Working_days->get();
}
if($workingDays){
$this->response($workingDays, REST_Controller::HTTP_OK);
}else{
$this->response([
'status' => FALSE,
'message' => 'No time of day data were found'
], REST_Controller::HTTP_NOT_FOUND);
}
}
}
我认为问题出在 .htaccess 文件中,但我不知道如何正确配置它。如何正确配置我需要的一切,以便进一步开发我的应用程序?
****更新**
我认为访问 http://myapp.dev/rest/index.php 是有效的,因为它显示 Codeigniters 404 页面(只是没有初始化任何初始控制器)
访问日志:
127.0.0.1 - - [09/Dec/2017:17:58:00 +0100] "GET /rest/ HTTP/1.1" 404 1130
127.0.0.1 - - [09/Dec/2017:18:00:54 +0100] "GET /rest/index.php HTTP/1.1" 404 1130
访问http://myapp.dev/rest/api/Workingday/workingDay时显示浏览器404页面
访问日志
127.0.0.1 - - [09/Dec/2017:17:53:18 +0100] "GET /rest/api/Workingday/workingDay HTTP/1.1" 404 228
【问题讨论】:
-
它可能是 RewriteRule,它将所有内容放在 url 行,并将其放在 index.php 之后。因此,如果您尝试转到
http://myapp.dev/rest/api/Workingday/workingDay,它会重写为http://myapp.dev/rest/index.php/rest/api/Workingday/workingDay...不确定是否有效。 (虽然我对 codeigniter 很陌生) -
不,它也不能与 url 中的 index.php 一起使用...我在配置 $config['index_page']='' 中有空白值
-
好吧,通过重写,如果您尝试访问
http://myapp.dev/rest/index.php... 应该将其更改为http://myapp.dev/rest/index.php/rest/index.php:( 哦...但是,是的,!-f的条件应该防止这种情况。好的。 -
那里有很多框架。结帐
http://myapp.dev/时指向哪个文件和位置? -
当提到自己(“我”)时,请始终使用大写字母。显然,在社交媒体和聊天室中使用小写字母很常见,但在这里我们热衷于可读性(希望您的 141 个其他问题没有以这种方式编写 - 这对志愿编辑来说是很多工作)。
标签: php apache .htaccess codeigniter vhosts