×
新网 > 建站推广 > 正文

yii url隐藏index.php

  • 作者:zccc
  • 来源:网络
  • 2020-09-07 12:04:35

yii url隐藏index.php的方法:首先在web根目录下增加“.htaccess”文件;然后修改配置“config/web.php”;最后将“AllowOverride None”修改为“AllowOverride All;”即可。推荐

yii url隐藏index.php的方法:首先在web根目录下增加“.htaccess”文件;然后修改配置“config/web.php”;最后将“AllowOverride None”修改为“AllowOverride All;”即可。

推荐:《PHP视频教程》《yii教程》

Yii 框架开启 URL 美化,隐藏 index.php [ 2.0 版本 ]

url美化

目的:将 http://localtest/yii/web/index.php?r=hello/index

美化成:http://localtest/yii/web/hello/index

这里我是用的wampserver新建了一个localtest站点(详情可点击这里点击这里),并将yii的basic文件夹重新命名为yii。

对比上面的两个地址,其实就是把index.php?r=隐藏。

这里分两步:

1、增加.htaccess文件

在web根目录下增加.htaccess文件,内容为:

RewriteEngine On
DirectoryIndex index.html index.php
# 如果是一个目录或者文件,就访问目录或文件
RewriteCond %{REQUEST_FILENAME} !-d
#如果文件存在,就直接访问文件,不进行下面的RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php

无法直接创建.htaccess,可以先创建一个txt文件,然后另存为…,保存为文件名为.htaccess,保存类型选择所有文件即可。

2、配置config/web.php 在config/web.php中的components数组中增加这一项:

'urlManager' => [
    // //开启url美化
    'enablePrettyUrl' => true,
    // //隐藏index.php
    'showScriptName' => false,
    // //禁用严格匹配模式
    'enableStrictParsing' => false,
    // //url后缀名称
    // 'suffix'=>'.html',
    'rules' => [
    ],
],

这时,可以将URL中的index.php?r=删除,如果出现404报错,可以查看服务器的配置,我用的是phpstudy中集成的apache 需要检查一下配置

conf\\httpd.conf中,开启 apache 的 mod_rewrite 模块

去掉 LoadModule rewrite_module modules/mod_rewrite.so 前的“#”符号;

然后修改 apache 的 AllowOverride

将 AllowOverride None 修改为 AllowOverride All;

由于我是在conf\\extra\\httpd-vhosts.conf中配置了站点,所以需要同步去httpd-vhosts.conf中将对应站的 AllowOverride None 修改为 AllowOverride All;

至此,我就可以用 http://localtest/yii/web/hello/index

来访问 http://localtest/yii/web/index.php?r=hello/index

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading