如何更改基本URLhttp://Domain/ProjectName/web/index.php到http://Doma


How to change base URL http://Domain/ProjectName/web/index.php to http://Domain/ProjectName in yii2

如何更改基本url:http://Domain/ProjectName/web/index.php到http://Domain/ProjectNameYii2中。

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

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

对于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] 

有关Yii2 中启用干净URL的更多信息,请参阅此

假设您有以下目录结构:

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

您可以做的是将项目文件夹移动到html之外。然后只复制html文件夹中应用程序的web文件夹,并将其重命名为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

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