Hide web from URL in Yii2

2019-05-23 02:57发布

问题:

I have followed answer described in this question. I have moved Application files and folder to one level up as suggested.

public_html/basic/web,config etc.. to public_html/web,config etc..

Removed index.php from url by modifying these configuration:

config/web.php

'urlManager' =>[
        'enablePrettyUrl' => true,
        'showScriptName' => false,
    ],

htaccess file in web folder

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

So now my url is example.com/web/controller/action

How can I hide web from the url above? I don't have access to the apache configuration file.

回答1:

My answer still stands :).

You are on the right track, move everything 1 directory lower. Right now you copied the entire yii2 application in public_html, you should move it 1 directory down. Then copy everything you have in web in public_html. By doing this all your scripts are not exposed to the web, only 1 index.php will be exposed and that is the proper way to have it set up.

Also this is the main tutorial for yii that tells you how to do it https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-shared-hosting.md

Do not be afraid of doing this, take the blue pill (or is it red) :).



回答2:

you can try to add in config\web

$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());

$config = [
'defaultRoute' => $baseUrl,
'components' => [
    'request' => [
        'baseUrl' => $baseUrl,
    ]
],
'urlManager' => [
    'baseUrl' => $baseUrl,
 ]