我在一个子目录上部署了yii2。 我在重定向上遇到问题。 在我的localhost上,我的项目不在子目录中,所以我没有任何问题。 但是当我将它部署在我们的实时服务器上并将项目放在子目录中时,我遇到了问题。
我的问题是,当我访问我网站的主页时,我被重定向到网站的根目录。
以下是一个示例:主站点: http : //example.com/
Yii2网站: http : //example.com/myproject/
当我尝试访问http://example.com/myproject/时 ,我希望在http://example.com/myproject/login上重定向,而不是重定向到http://example.com/login 。
我已经将我的.htaccess改成了这个
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]
但我认为这个错误虽然......
我的web.php上也有这个
$config = [
'id' => 'basic',
'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
'defaultRoute' => 'site/login',
//... other codes here...
正如你所看到我有一个defaultRoute
是site/login
但似乎我继续重定向/login
,这不是在子文件夹链接。
任何帮助如何在子目录中设置yii2
对你的帮助表示感谢。 谢谢!
UrlManager不知道该应用程序不在Web服务器的根目录中。 尝试将$config['components']['urlManager']['baseUrl']
为项目的路径:
// in web.php
$config = [
'id' => 'basic',
//... other codes here...
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'baseUrl' => 'myproject',
]
]
]
顺便说一下,你可以缩短你的basePath
定义
'basePath' => dirname(__DIR__),
应该返回相同的目录与较少(和恕我直言清洁)代码。
尝试通过设置项目文件夹的路径来使用.htaccess
文件的RewriteBase参数:
RewriteEngine on
RewriteBase /myproject
# 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
RewriteRule ^(.*) system/index.php/$1 [QSA,L]
我有同样的问题,修改.htaccess
有奇怪的效果,
urlManager.baseUrl配置修复url创建和根,
assetManager.baseUrl config修复脚本/样式路径
'components' => [
'urlManager' => [
'baseUrl' => '/myproject/',
...
],
'assetManager' => [
'baseUrl' => '@web/myproject/assets',
],
...
]