yii2.0預設的訪問形式為:dxr.com/index.php?r=index/list,一般我們都會配置成pathinfo的形式來訪問:dxr.com/index/list,這樣更符合使用者習慣。
具體的配置方法為:
一.配置yii2.0。
開啟config目錄下的web.php,在$config = [ 'components'=>[ 加到這裡 ] ]中加入:
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ ],],
此時,yii2.0已經支援以pathinfo的形式訪問了,如果此時訪問不了,繼續往下看。
二.配置web伺服器。
1.如果是apache,在入口檔案(index.php)所在的目錄下建立一個文字檔,接著另存新檔.htaccess,用記事本開啟此檔案加入:
RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . index.php
儲存即可。
2.如果是nginx,在nginx設定檔中加入:
server { listen 80; server_name localhost; location / { root E:/wwwroot/yii2.0; index index.html index.php; if (!-e $request_filename){ rewrite ^/(.*) /index.php last; } } location ~ \.php$ { root E:/wwwroot/yii2.0; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}
三:重啟web伺服器。
至此,配置完畢。