当前位置: 首页 > 后端技术 > PHP

Yii2以pathinfo的形式配置url

时间:2023-03-29 16:25:05 PHP

Yii2.0默认的访问形式是:my.oschina.net/index.php?r=post/index。一般我们会配置成以pathinfo的形式访问,比如:my.oschina.net/post/index,比较符合用户习惯。1、配置yii打开config目录下的web.php,在$config=['components'=>[]]中添加如下内容:'urlManager'=>['enablePrettyUrl'=>true,'showScriptName'=>false,'rules'=>[],]如果该配置项在配置文件中已经存在,但是被注释掉了。只需删除评论。至此,yii2.0已经支持以pathinfo形式访问。但是,路径仍然是这样的:my.oschina.net/index.php/post/index。接下来,我们希望删除index.php。二、配置http服务器1、Apache在入口文件(index.php)所在目录下新建一个。文本文件,然后保存为.htaccess,用编辑器打开这个文件并添加:RewriteEngineonRewriteCond%{REQUEST_FILENAME}!-fRewriteCond%{REQUEST_FILENAME}!-dRewriteRule。就是/conf/vhosts/test.conf文件)并添加:location/{try_files$uri$uri//index.php?$query_string;}整个服务器配置类似:server{listen80;server_nametest.yii.com;根“/Projects/yii/web”;location/{indexindex.htmlindex.htmindex.php;try_files$uri$uri//index.php?$query_string;}error_page500502503504/50x.html;location=/50x.html{根html;}location~\.php(.*)${fastcgi_pass127.0.0.1:9000;fastcgi_indexindex.php;fastcgi_split_path_info^((?U).+\.php)(/?.*)$;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;fastcgi_paramPATH_INFO$fastcgi_path_info;fastcgi_paramPATH_TRANSLATED$document_root$fastcgi_path_info;包括fastcgi_params;