Yii 2.0 hiding /basic/web from the URL along with the index.php

前端 未结 3 733
轮回少年
轮回少年 2021-01-06 17:09

Plenty of information around the net on how to hide the index.php from your Yii 2.0 application URL, however, what I\'m trying to do here is to also remove the \'/basic/web/

相关标签:
3条回答
  • 2021-01-06 17:43

    The Working Solution is Here!

    Step 1. in yii2 basic application directory create an index.php file and fill it as below

    <?php
    
    // comment out the following two lines when deployed to production
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
    
    require __DIR__ . '/vendor/autoload.php';
    require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
    
    $config = require __DIR__ . '/config/web.php';
    
    (new yii\web\Application($config))->run();
    

    Step 2. Create a .htaccess file in the same base directory as follows

    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 . index.php
    
    0 讨论(0)
  • 2021-01-06 17:53

    You should define your apache configuration in another way. Your site should point to {folder}/basic/web and not to {folder}.

    Because you changed the requirements:

    For a cpanel setup you should:
    1) remove the silly basic folder, what is the point of it anyway? Just because Yii installs that way does not mean you have to keep it. So move everything 1 level up.
    2) Rename web to public_html make sure you rename it in some files too (config/bootstrap comes to mind).

    Yes you can do it with .htaccess but you should not have the files exposed to the internet, just your web folder should be exposed so I am not giving you that solution because it is not a good one.

    0 讨论(0)
  • 2021-01-06 17:53
    RewriteEngine on
    # Change yourdomain.com to be your primary domain.
    
    RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
    
    RewriteCond %{REQUEST_URI} !^/basic/web/
    
    # Don't change this line.
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ /basic/web/$1
    
    RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$ 
    RewriteRule ^(/)?$ basic/web/index.php [L]
    

    Change .htaccess permissions to 440 when you're done. Nothing to fear using this method, contrary to what is stated by Mihai P.

    0 讨论(0)
提交回复
热议问题