当前位置:Gxlcms > php框架 > yii2.0实现pathinfo的形式访问的配置方法

yii2.0实现pathinfo的形式访问的配置方法

时间:2021-07-01 10:21:17 帮助过:101人阅读

yii2.0默认的访问形式为:dxr.com/index.php?r=index/list,一般我们都会配置成pathinfo的形式来访问:dxr.com/index/list,这样更符合用户习惯。

具体的配置方法为:

一.配置yii2.0。

打开config目录下的web.php,在$config = [ 'components'=>[ 加到这里 ] ]中加入:

  1. 'urlManager' => [
  2. 'enablePrettyUrl' => true,
  3. 'showScriptName' => false,
  4. 'rules' => [
  5. ],
  6. ],

此时,yii2.0已经支持以pathinfo的形式访问了,如果此时访问不了,继续往下看。

二.配置web服务器。

1.如果是apache,在入口文件(index.php)所在的目录下新建一个文本文件,接着另存为.htaccess,用记事本打开此文件加入:

  1. RewriteEngine on
  2. RewriteCond %{REQUEST_FILENAME} !-f
  3. RewriteCond %{REQUEST_FILENAME} !-d
  4. RewriteRule . index.php

保存即可。

2.如果是nginx,在nginx配置文件中加入:

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. root E:/wwwroot/yii2.0;
  6. index index.html index.php;
  7. if (!-e $request_filename){
  8. rewrite ^/(.*) /index.php last;
  9. }
  10. }
  11. location ~ \.php$ {
  12. root E:/wwwroot/yii2.0;
  13. fastcgi_pass 127.0.0.1:9000;
  14. fastcgi_index index.php;
  15. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  16. include fastcgi_params;
  17. }
  18. }

三:重启web服务器。

至此,配置完毕。

人气教程排行