Yii: .htaccess and urlManager for separate backend and frontend - .htaccess

I'm having a hard time to configure my .htaccess and the urlManager in a Yii project to have the frontend in http://www.example.com and the backend in http://www.example.com/backend with the following folder structure. Any help is welcome. Thanks.
/assets
/backend
/controllers
/config
main.php
/models
/views
/common
/models
/protected
/controllers
/config
main.php
/models
/views
.htaccess
backend.php
index.php
Solution: after the great help of #bool.dev everything it's working, so I'm adding here every needed final file. In the frontend I'm using path format for the url and hiding the index.php
/backend/config/main.php
$backend=dirname(dirname(__FILE__));
Yii::setPathOfAlias('backend', $backend);
return array(
'basePath' => $backend,
'controllerPath' => $backend.'/controllers',
'viewPath' => $backend.'/views',
'runtimePath' => $backend.'/runtime',
...);
/protected/config/main.php
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
.htaccess
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /yii/example/
RewriteRule backend backend\.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>
backend.php
$yii=dirname(__FILE__).'/../../yii/framework/yii.php';
$config=dirname(__FILE__).'/backend/config/main.php';
require_once($yii);
Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common');
Yii::createWebApplication($config)->run();
index.php
$yii=dirname(__FILE__).'/../../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
require_once($yii);
Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common');
Yii::createWebApplication($config)->run();

According to this wiki article by Qiang, you could make the following changes and it should work:
// backend.php:
require('path/to/yii.php');
Yii::createWebApplication('backend/config/main.php')->run();
Then in your backend's config (i.e backend/config/main.php):
$backend=dirname(dirname(__FILE__));
$frontend=dirname($backend);
Yii::setPathOfAlias('backend', $backend);
return array(
'basePath' => $backend,
'controllerPath' => $backend.'/controllers',
'viewPath' => $backend.'/views',
'runtimePath' => $backend.'/runtime',
'import' => array(
'backend.models.*',
),
// ... other configurations ...
);
But for this to work we need the main .htaccess to route example.com/backend to backend.php, which i haven't figured out yet.
Edit:Just figured out:
RewriteEngine On
RewriteBase /projectroot/
RewriteRule backend backend\.php [T=application/x-httpd-php]
The RewriteBase was important for me, as the backend.php was not found when i hadn't given the correct projectroot, basically it should be the directory where you have the entry script backend.php.

Related

Yii2 Pretty Url don't work in Share Hosting (Basic Template)

I m trying to deploy a small project build with Yii2 (Basic Template) in a Share Hosting enviroment. Everything is working fine in local.
The Host server was like below in the root directory:
/
---logs
---index.html
After reading the doc of Yii2. I then delete the file index.html and upload my project folders into the root directory. It s now looking like this:
/
---logs
---assets
---commands
---config
---controllers
--- ...
---views
---web
--- ...
After trying the website i got a blank page. I then decided to create a htaccess file in the root of host server to point the web folder like here
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
After this i can get the index page very well with
www.example.com
and I get a blank page when i try to access for example (with url prettyUrl activated)
www.example.com/articles/list
But When I deactivate the prettyUrl, it s working like for example
www.example.com/web/index.php?r=articles/list
The .htaccess in the web folder is like this:
Options +FollowSymLinks
IndexIgnore */*
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
# Disable Directory Browsing
Options All -Indexes
and the url rules are set in the web.php file like below:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'baseUrl' => '/',
'rules' => [
...
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
The main majority of the shared hosts disables some of the directives from the htaccess file to prevent some security issues. And if a restricted directive is found in your htaccess file you will get an 500 error page.
It's very likely that if you remove the Options +FollowSymLinks directive everything will work.
Additionally you can check out the apache error log for more information.

Yii and .htaccess file on production server

I am new to yii and have some problem in configuring .htaccess file on production server.
On localhost :
Location of Application : /www/connect_donors/
Default URL that yii provides is,
http://localhost/connect_donors/index.php?r=controllerId/functionName
We used the urlManager in /connect_donors/protected/config/main.php to configure the SEO friendly url's..
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
Now the URL that was working was
http://localhost/connect_donors/index.php/controllerId/functionName
Then I used the .htaccess file to remove index.php from the above URL.
Location of .htaccess is : /connect_donors/.htaccess
Following is .htaccess file,
RewriteEngine On
RewriteBase /connect_donors
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
URL Chenged to :
http://localhost/connect_donors/controllerId/functionName
Everything working fine and awesome.
But yesterday I uploaded the application on production server.
On Production Server
Everything remained same only I had to change the .htaccess file.
The .htaccess file on server is,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Now the following url:
http://donorsconnect.com/
loads the home page of server properly.
But,
http://donorsconnect.com/profile
redirects again to home page.
NOTE : There is no session set on the "profile" controller.
class ProfileController extends CController
{
public function actionIndex()
{
$this->render('index');
}
}
I tried lot of things, changing the .htaccess file to different codes. but none helped me.
Any help is appreciable.
Solution
I finally got the solution and the mistake I had done.
My Components had a request array containing baseUrl.
'components'=>array(
...
'request' => array(
'baseUrl' => 'http://donorsconnect.com',
...),
Due to this it was not loading. I did not find the real reason for that.
But after removing that 'request' array, its loading fine.
Check link,
http://donorsconnect.com/profile
You should not hide index.php like this, read carefully : http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
You should set showScriptName to false in your main config :
'urlManager'=>array(
.....
'showScriptName'=>false,
.....
),
And your .htaccess should look like this :
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

what is wrong with this .htaccess and Yii urlManager settings?

I have a yii website under a sub directory e.g
http://localhost/~username/maindirectory/yiiapp/
My htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /yiiapp
# 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>
protected/config/main.php
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
....
The issue:
let say I accesss :
step one: http://localhost/~username/maindirectory/yiiapp/
step two: click any link on home page (default yii app) let say About Us page
step three: http://localhost/~username/maindirectory/yiiapp/site/page?view=about
step four : click on any on the link (let say same page)
url will look like : http://localhost/yiiapp/site/page?view=about
so : all links are accessible as : http://localhost/yiiapp/.... , instead of removing index.php from link the while string b/w localhost and base directory is removed .
I tried this already and need the same sort of url on localhost , obviously without using sub domains
please help me fix this .
Partial answer:
The problem is that you are redirecting to an absolute URL on the same domain.
The URL probably looks like /yiiapp/site/page?view=about. However that leads to http://localhost/yiiapp/site/page?view=about. You must prepend the website directory relative to the domain (not sure if I expressed properly).
For example, the URL in the a href tag should look like /~username/maindirectory/yiiapp/site/page?view=about so you must prepend the /~username/maindirectory part from somewhere.
Have your tried:
'urlManager'=>array(
....
'baseUrl' => '/~username/maindirectory/yiiapp',
....
http://www.yiiframework.com/doc/api/1.1/CUrlManager#setBaseUrl-detail
so RewriteBase /~username/maindir/yiiapp , in .htaccess fixed the issue , not the
'baseUrl' => '/~username/maindirectory/yiiapp' thing in config/main.php
Here is the full .htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /~username/maindir/yiiapp
# 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>

How to remove index.php from URLs in Kohana 3.1

Currently, using Kohana 3.1, I can access my controllers using:
http://localhost/kohana/index.php/admin
However, I would like to access them without the "index.php" in the middle, as in:
http://localhost/kohana/admin
How can I do that? Do I need to change my .htaccess file or some config option?
I'm using the .htaccess provided with Kohana:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
Order Deny,Allow
Deny From All
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
However, if you use Rewritebase /kohana/, you will still get index.php in the url.
I'm using Kohana 3, so I go to bootstrap.php and change this:
Kohana::init(array(
'base_url' => '/',
'index_file' => '',
));
Change the RewriteBase directive to where the application is. Yours would be:
# Installation directory
RewriteBase /kohana/
Also make sure you have the correct base_url in your bootstrap.
On some website I saw .htaccess suggestion:
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php?kohana_uri=$0 [PT,L,QSA]
It seems that you miss kohaha folder.
It should be like this:
Kohana::init(array(
'base_url' => '/kohana/',
'index_file' => '',
));
Turn on URL rewriting by adding the following code:
In .htaccess:
RewriteEngine On
# Installation directory
RewriteBase /
In Bootstrap:
Kohana::init(array( 'base_url' => '', 'index_file' => '' ));
You need to use 'index_file' => FALSE in bootstrap.php
Source : http://kohanaframework.org/3.0/guide/kohana/tutorials/clean-urls

Remove "/index/" from URIs in Kohana 3

As of right now all of my controllers are mapped like this:
http://example.com/index/index
http://example.com/index/services
http://example.com/index/contact
What I want to do is change the configuration of the URI to look like this:
http://example.com/index
http://example.com/services
http://example.com/contact
Here is my .htaccess file
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
My bootstrap.php has the following:
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE
));
How can this be achieved?
Your .htaccess code looks fine. Check that your Apache configuration is fine:
Kohana URL rewriting
Also, you need to edit your application/bootstrap.php and setup the initialization variables there.
Kohana::init( array(
'base_url' => '/',
'index_file' => FALSE,
) );
You may want to check the user guide, there is a page just for this setup.
http://kohanaframework.org/3.0/guide/kohana/tutorials/clean-urls
I assume that you use action_index, action_services and action_contact inside Controller_Index.
If so, all you need to do is remove the controller section from routes (in your bootstrap.php)
Route::set('posttype', '<param>', array('action' => 'index|services|contact'))
->defaults(array(
'controller'=> 'index',
'action' => 'index'
));

Resources