【问题标题】:How to change base URL http://Domain/ProjectName/web/index.php to http://Domain/ProjectName in yii2如何在 yii2 中将基本 URL http://Domain/ProjectName/web/index.php 更改为 http://Domain/ProjectName
【发布时间】:2023-04-01 12:24:01
【问题描述】:

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

【问题讨论】:

    标签: php apache http yii2


    【解决方案1】:

    您应该在基本模板中的/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] 
    

    查看更多Enable clean URL in Yii2

    【讨论】:

      【解决方案2】:

      假设您有以下目录结构:

      /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

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

      【讨论】:

        猜你喜欢
        • 2020-09-03
        • 2011-01-16
        • 2021-01-02
        • 2017-09-28
        • 2021-05-20
        • 2019-09-06
        • 1970-01-01
        • 2013-09-17
        • 1970-01-01
        相关资源
        最近更新 更多