I'm implementing a RESTful service using Yii2 advanced template following this guide . However, I'm ended up getting the following structure for the REST service:
http://localhost/MyProject/api/web/v1/user/1
whereas it should be http://localhost/MyProject/api/v1/user/1
.
How can I remove the /web/
from the URL path?
If you use apache in .htaccess
<IfModule mod_rewrite.c>
#Send all /api requests to api/web folder
RewriteRule ^api/(.*)$ api/web/$1
</IfModule>
If you are going to host it on your own server, just create a virtual host pointing to the api/web
and you will have it sorted. Here is a sample
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName api.websiteurl.com DocumentRoot /var/www/html/app/api/web </VirtualHost>
With that, I can access my api from api.websiteurl.com
If you want to api to be accessed from websiteurl.com
, then just set the document root of your server to point to api/web
You add below code in web.php file...
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
And Then add one file .htaccess file in web folder..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
I actually like to use Kartik's Practical App or Practical-A App . They are based on the yii2-advanced template, but with some changes, like getting rid of the /web directory
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.