Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying yii2 in a subdirectory

Tags:

php

yii

yii2

I deployed yii2 on a subdirectory. I am encountering problems on the redirection. On my localhost, i worked my project not in a subdirectory, so I am not having any problems. But when I deployed it on our live server and put the project in a subdirectory, I am having problems.

My problem is that when I visit the homepage of my site I am being redirected to the root of the website.

Here's an example: Main site: http://example.com/

Yii2 Site: http://example.com/myproject/

When I try to go to http://example.com/myproject/, I am expected to be redirected at http://example.com/myproject/login, instead I am redirected to http://example.com/login.

I already changed my .htaccess into this one

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 ^(.*) system/index.php/$1 [QSA,L]

But i think this one is wrong though...

I also have this one on my web.php

$config = [
    'id' => 'basic',
    'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
    'defaultRoute' => 'site/login',
     //... other codes here...

As you can see i have a defaultRoute to be site/login but it seems i keep on redirecting on the /login, which is not in a subfolder link.

Any help how to set up yii2 in a subdirectory

Your help will be greatly appreciated. Thanks!

like image 299
PinoyStackOverflower Avatar asked Jul 22 '15 15:07

PinoyStackOverflower


People also ask

How to configure yii2 server site with directory protection?

It's easy to configurate a Yii2 server site with directory protection: With using Sub Directory for Yii2, you could set sub-directory path into yii2's config: After that, it would be correct to bind URL when you use Url generator: With same path included /public, for example: /sublara/

Is the directory structure used by Yii appropriate for my project?

In this article, we describe the directory structure used by yiiframework.com - the official Yii framework website. While this structure may look overly complicated for small projects or may not be optimal in some sense, we believe it is appropriate for medium or large projects in a team development environment.

What is the backend directory in Yii?

The backend directory is a complete Yii structure, similar to what you will find in the Basic Template. You will find the following subdirectories: assets config controllers models runtime

What is the configuration folder in Yii?

This folder holds the configuration files that include things like connecting Yii with your database, setting up e-mail sending, reformatting URLs and setting globally available parameter values.


Video Answer


1 Answers

The UrlManager doesn't know that the app is not in the webserver's root directory. Try setting your $config['components']['urlManager']['baseUrl'] to your project's path:

// in web.php
$config = [
    'id' => 'basic',
    //... other codes here...
    'components' => [
        'urlManager' => [
            'class' => 'yii\web\UrlManager',        
            'baseUrl' => 'myproject',
        ]
    ]
]

BTW, you could shorten your basePath definition to

'basePath' => dirname(__DIR__),

which should return the same directory with less (and IMHO cleaner) code.

like image 150
tarleb Avatar answered Oct 02 '22 16:10

tarleb