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

99封情书 提交于 2019-11-29 07:49:54

For advanced application follow these steps:

1) Add the following htaccess to frontend/web

RewriteEngine on

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php

2) Add the following htaccess to root folder where application is installed

# prevent directory listings
Options -Indexes
IndexIgnore */*

# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1

3) Edit your frontend/config/main.php file with the following at the top

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

4) Add the request component to the components array in the same file i.e frontend/config/main.php

'components' => [
        'request' => [
            'baseUrl' => $baseUrl,
        ],
],

That's it.Now you can access the frontend without web/index.php

For you second question you need to write the rule for it in your URL manager component.

Something like this:

'urlManager' => [
            'baseUrl' => $baseUrl,
            'class' => 'yii\web\UrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'enablePrettyUrl' => true,
            'rules' => array(
                    'transaction/getrequestdetail/<id>' => 'transaction/getrequestdetail',
           ),
],

If it still isn't working after going through the answers above, then you can edit your 'apache2.conf' file in your favorite editor to change

AllowOveride None to AllowOveride All

On Ubuntu, the file is located at /etc/apache2/apache2.conf

The final edit should look like

<Directory /var/www/>
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
</Directory>

Finally, restart the Apache server

You can add the information in file configuration to remove /web:

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

return [
    ...
    'components' => [
            'request' => [
                'baseUrl' => $baseUrl,
     ],
      ...
    ]
]

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!