Yii2.0默认的访问形式为:my.oschina.net/index.php?r=post/index,通常咱们都会配置成pathinfo的形式来访问,形如:my.oschina.net/post/index,这样更符合用户习惯。php
打开config目录下的web.php,在$config = [ 'components'=>[] ]中加入如下内容:html
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ ], ],
若是配置文件中已经有了该配置项,可是被注释掉了。将其注释去掉便可nginx
此时,yii2.0已经支持以pathinfo的形式访问了。不过路径仍是形如:my.oschina.net/index.php/post/indexweb
咱们接下来但愿把index.php去掉服务器
在入口文件(index.php)所在的目录下新建一个文本文件,接着另存为.htaccess,用编辑器打开此文件加入:yii2
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php
保存便可yii
在nginx配置文件(我本地是/conf/vhosts/test.conf文件)中加入:编辑器
location/{ try_files $uri $uri/ /index.php?$query_string; }
整个server配置相似:post
server { listen 80; server_name test.yii.com; root "/Projects/yii/web"; location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } }
至此,配置完毕。url