yii2 rest api url rewriting (hide directories) - apache

I would like to use clean urls on my yii2 rest api.
I'm having clean urls for my frontend application but I'm failing to create clean urls for my yii2 rest api application.
I created the rest api by following the tutorial (Yii2: RESTful api: tutorial). The rest api uses a module for versioning.
Two .htaccess files have been created.
One file in my root and an another one in my "api/web" directory.
root .htaccess file
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/web' .htaccess file
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]
Browsing to 'http://api.localhost.com/v1/music' gives a 404 page not found error.
While 'http://api.localhost.com/api/web/v1/music' returns results
(I would like to hide 'api/web/' in my url)

'request' => [
'baseUrl' => str_replace('/api/web', '', (new \yii\web\Request())->getBaseUrl()),
],
Add this request component config in your main api config

Configure the host file to look in /project_folder/api/web

Related

Yii2 | Redirect to backend/frontend with htaccess

I am using Yii2 advanced template on localhost and I want to redirect the user to backend/web when the url is localhost/site/admin and redirect to frontend/web when the url is localhost/site using .htaccess file.
I enabled apache rewrite and I have tried these solutions
advance template Yii2 - redirect to frontend/web using htaccess
Yii2 .htaccess redirect to backend part
Redirecting to backend via htaccess Yii2
but in all cases I get 404 Error when the url is localhost/site/admin
Not Found
The requested URL /site/admin was not found on this server.
and when the url is localhost/site the content of the root directory of my site is displayed to me.
Now my .htaccess in the root of project is :
# prevent directory listings
Options -Indexes
IndexIgnore */*
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1
and in backend/web and frontend/web is:
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
Although I had enabled mod_rewrite through the terminal via the following command:
sudo a2enmod rewrite
But it was not enabled and I manually activated it from ‍/etc/apache2/apache2.conf.
use it without site
localhost/admin

Htaccess mod rewrite don't work

I have a project with two applications: a frontend (in AngularJs) and a backend (in Phalcon). I my server document root i have two folders and one htaccess:
public_html
- api
- controllers
- index.php
- app
- .htaccess
The .htaccess have the next configuration:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/(.*)$ api/index.php?_url=/$1 [QSA,L]
</IfModule>
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
So, when i send a POST request (for example) to http://mydomain/api/sessions, the result is 404 not found. The router configuration is:
$router->addPost('/sessions', array(
'controller' => 'sessions',
'action' => 'post'
));
In my localhost works fine with this configuration. But, in my VPS not.
Any ideas ?
Update 1:
If i don't use any REST service, accessing via http://mydomain/api the Phalcon index controller is loaded.
Update 2:
If i try to access a REST service using a Phalcon url like http://mydomain/api/index.php?_url=/licenses works fine.
i found the problem. the virtual host configuration file had not the next lines:
<Directory "/var/www/domain/public_html">
Options All
AllowOverride All
Allow from all
</Directory>
You can sometimes get away with relative path substitutions in the document root, however, you should make the RewriteRule substitution root-relative (starts with a slash), or specify a RewriteBase / directive after you enable the rewrite engine in order to explicitly specify the URL prefix.
RewriteRule ^api/(.*)$ api/index.php?_url=/$1 [QSA,L]
Becomes:
RewriteRule ^api/(.*)$ /api/index.php?_url=/$1 [QSA,L]

Yii framework: Using htaccess for site in sub directory not working on Hostgator

I'm trying to set up Yii site to be loaded from sub directory to root domain. In my root site folder I have only root .htaccess file and sub directory "subdir" which contains Yii site. I found a solution that works on my local environment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
But when I upload the site to HostGator it just does not work correctly
For example
if I use http://localhost/contact on my local environment, correct page is opened (called site/contact - site controller and contact action. I've added Yii 'contact'=>'site/contact' rule to the urlManager, so both http://localhost/contact and http://localhost/site/contact can work)
If I use http://mydomain.com/contact or (http://mydomain.com/site/contact) on HostGator, I get default index page (called site/index - site controller and default index action instead)
When I choose to access subsites directly like http://mydomain.com/subdir/contact it works fine, but it does not work if I use http://mydomain.com/contact
I guess that I need somehow to change this last rule in htaccess, but not sure how. Thanks!
I've found alternative solution that works. I was forced to use old fashioned URLs instead of paths, so I've changed my urlManager to:
'urlManager'=>array(
//'urlFormat'=>'path',
'showScriptName'=>true,
'caseSensitive'=>false,
'rules'=>array(
...
)
)
And my root htaccess looks now like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/index.php?r=$1 [QSA,L]
So, when accessing
http://mydomain.com/site/contact
Actual mapped URL is
http://mydomain.com/subdir/index.php?r=site/contact
which as result returns correct contact page
Since, you stated in your previous question that www.mysite.com/mysite/frontend/www/controller/action works fine; you should be using:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)$ /mysite/frontend/www/$1 [L]
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php under webroot (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory

.htaccess mod_rewrite dynamic match with Yii generated urls

I'm trying to use Apache's mod_rewrite module to make dynamically generated urls from a Yii app, which always contain index.php as the router/bootstrap, into static urls through .htaccess available to browse through a web browser.
Example: localhost/projectname/index.php/commentfeed.xml is the dynamically generated page, but I want it to be able to be accessed through localhost/projectname/commentfeed.xml
Apache version I'm running is: Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.0 through XAMPP for my development platform. The mod_rewrite module is present and loaded in Apache's httpd.conf file.
My .htaccess in localhost/projectname/ is as follows:
#Turning on the rewrite engine is necessary for the following rules and features.
#FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.so>
Options +FollowSymlinks
RewriteEngine On
#Unless an explicit file or directory exists,
#redirect all request to Yii entry script.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Now, when I try to access commentfeed.xml through my browser without index.php (localhost/projectname/commentfeed.xml) I get a 404 error - Object not found!.
I've looked over the following guides & SO questions: URL Rewriting Guide, mod_rewrite documentation, and SO: Tips for debugging .htaccess rewrite rules but none of them had explicitly what I was looking for. Also, my apache error log is not reporting anything about the .htaccess file. Any help or guidance towards the correct usage would be greatly appreciated.
You need to tell the Yii UrlManager that the script name is not required. In your config/main.php file you should have:
'components'=>array(
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
...
),
),
),
Make sure you also have a route defined to specify the controller/action that generates the commentfeed.xml file.
More info: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
The specific answer to my problem lay in using <IfModule> ... </IfModule>. With my setup of XAMPP v3.2.1 and the apache server installed with it, the only lines of code I needed in the .htaccess was simply:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

Yii framework, how to define a url for backend

I'm developing a CMS using yii framework. There is a frontend and backend. I want the users to be able to access the backend like this: http://www.mysite.com/admin, right now it is working like this: http://www.mysite.com/admin.php.
For the backend I have defined different section with it's own config, controller and ... and the page for accessing the backend in admin.php
here is my directory structure:
...
admin
--components
--config
---main.php
--controllers
---NewsController.php
---ShowCOntroller.php
---SiteController.php
--models
---LoginForm.php
---News.php
---Show.php
---User.php
--runtime
...
--Views
---layouts
---news
---show
---site
protected
-commands
-data
-extentions
-messages
-migrations
-models
-modules
--image
-runtime
-views
themes
uploads
admin.php
index.php
.htaccess
And here is my .htaccess file:
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule admin admin\.php [T=application/x-httpd-php]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>
You can have a look of how did I solved with the help of #bool.dev Yii: .htaccess and urlManager for separate backend and frontend.
I hope it helps.
One thing you could consider is creating the backend as a Yii module inside the frontend. A module is like an application within an application. You would create models, controllers and views, possibly reusing code from the frontend. The URLs for accessing the module would include the module name; for example http://www.example.com/admin/controller/action.
On the other hand, if you just want to map requests made to /admin go to the script admin.php, you can do it in mod_rewrite. For example:
# .htaccess
RewriteEngine On
RewriteRule ^admin admin.php # route requests for /admin to admin.php
# Rest of Yii's rewrite rules:
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
I would also suggest going the module way.
Then you can use the URL manager to manage your URL for the admin.
You really need to direct all requests through index.php.
Use the URL manager to route admin requests. Have a look here, http://www.yiiframework.com/doc/guide/1.1/en/topics.url
I suggest you to use Yii Module base concepts. So the Admin page will be a module on your application.
Please see this link http://www.yiiframework.com/doc/guide/1.1/en/basics.module
Use yii Url Manager which is best at all and if all the configuration and controllers are different for the backend then you can build a controller with named AdminController.