yii2 rest api url rewriting(隐藏目录)

时间:2017-06-22 20:57:47

标签: apache .htaccess yii2 clean-urls yii-url-manager

我想在yii2 rest api上使用干净的网址。

我为我的前端应用程序提供了干净的网址,但是我没有为我的yii2 rest api应用程序创建干净的网址。

我按照教程(Yii2: RESTful api: tutorial)创建了其余的api。其余的api使用模块进行版本控制。

已创建两个.htaccess文件。 我的根目录中的一个文件和我的" api / web"中的另一个文件。 。目录

root .htaccess文件

RewriteEngine on
# prevent directory listings
Options -Indexes
IndexIgnore */*

# follow symbolic links
Options FollowSymlinks

RewriteCond %{HTTP_HOST} ^api.localhost [NC]
RewriteRule ^(.*)$ api/web/$1 [L,PT]

' API /网络' .htaccess文件

RewriteEngine on

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule ^(.*)\?*$ index.php?r=$1 [L,QSA]

浏览' http://api.localhost.com/v1/music'给出了404页面找不到错误。

虽然' http://api.localhost.com/api/web/v1/music'返回结果

(我想在我的网址中隐藏' api / web /'

2 个答案:

答案 0 :(得分:1)

   'request' => [
        'baseUrl' => str_replace('/api/web', '', (new \yii\web\Request())->getBaseUrl()),

    ],

在主api配置中添加此请求组件配置

答案 1 :(得分:-2)

配置主机文件以查找/ project_folder / api / web

相关问题