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

后端 未结 4 650
栀梦
栀梦 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:57

    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/' => 'transaction/getrequestdetail',
               ),
    ],
    

提交回复
热议问题