how to remove url (/web/index.php) yii 2 and set route with parameter with clean url?

后端 未结 4 653
栀梦
栀梦 2020-12-17 03:11

first question: i already remove index.php, but i want remove /web also. this is my .htaccess

RewriteEngine on
# If a         


        
4条回答
  •  心在旅途
    2020-12-17 03:56

    Using str_replace('/frontend/web', '', (new Request)->getBaseUrl()) for detecting base URL is a bad idea and using str_replace('/web', '', (new Request)->getBaseUrl()) is a terrible idea. str_replace() removes all occurences of requested string, so str_replace('/frontend/web', '', (new Request)->getBaseUrl()) for URL /frontend/web/tools/frontend/webalizer will give you /toolsalizer. Definitely not a thing that you want.

    If you want to remove this string only from the begging of URL:

    $baseUrl = (new Request())->getBaseUrl();
    if ($baseUrl === '/frontend/web') {
        $baseUrl = '';
    } elseif (strncmp($baseUrl, '/frontend/web/', 14) === 0) {
        $baseUrl = substr($baseUrl, 13);
    }
    

    But the best solution would be avoiding the whole problem in the first place, for example by using symlinks to mimic required directory structure for single webroot.

提交回复
热议问题