Yii2配置pathinfo形式的url

Yii2.0默認的訪問形式爲:my.oschina.net/index.php?r=post/index,通常咱們都會配置成pathinfo的形式來訪問,形如:my.oschina.net/post/index,這樣更符合用戶習慣。php

1、配置yii

打開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去掉服務器

2、配置http服務器

一、Apache

在入口文件(index.php)所在的目錄下新建一個文本文件,接着另存爲.htaccess,用編輯器打開此文件加入:yii2

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

保存便可yii

二、Nginx

在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;
        }
    }

3、重啓http服務器

至此,配置完畢。url

相關文章
相關標籤/搜索