如何在yii2中将基本URL http://Domain/ProjectName/web/index.php更改为http:// Domain / ProjectName

时间:2016-05-05 18:41:53

标签: php apache http yii2

如何在Yii2中将基本网址:http://Domain/ProjectName/web/index.php更改为http://Domain/ProjectName

2 个答案:

答案 0 :(得分:0)

你应该在基本模板中的/web.php的app / config / main.php(在高级模板中)启用漂亮的URL

 $config = [
    //...
    'components' => [
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'showScriptName' => false,
            'enablePrettyUrl' => true,
        ],
        //...

for apache将此添加到ProjectName / web / .htaccess文件

Options +FollowSymLinks
IndexIgnore */*

RewriteEngine on
RewriteBase /ProjectName/ 
# 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?r=$1 [L,QSA] 

了解更多Enable clean URL in Yii2

答案 1 :(得分:0)

假设您有以下目录结构:

/root
|
|--/logs // or any other folder
|--/html <-- this is where DOMAIN.com is pointing
   |
   |--/ProjectName
      |
      |--/assets
      |--/web
      |--// and other folder

您可以做的是将项目文件夹移到html之外。然后只复制web文件夹中应用的html文件夹,并将其重命名为ProjectName(或任何其他)。现在您的文件夹结构应如下所示:

/root
|
|--/logs // or any other folder
|--/html <-- this is where DOMAIN.com is pointing
   |
   |--/ProjectName
      |
      |--/assets
      |--/css
      |--/index.php <-- EDIT this one
      |--// and other files you had in web folder
|--/ProjectName
   |
   |--/assets
   |--/web
   |--// and other folder

最后打开index.php(上面指出的文件)并替换以下行:

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

用这个:

$project_path = __DIR__ . '/../../PojectName';

require($project_path . '/vendor/autoload.php');
require($project_path . '/vendor/yiisoft/yii2/Yii.php');

$config = require($project_path . '/config/web.php');

就是这样。现在,您可以通过以下网址访问您的申请:http://DOMAIN/ProjectName

希望有所帮助。如果没有在评论中告诉我。