Home >PHP Framework >YII >yii url hide index.php

yii url hide index.php

藏色散人
藏色散人Original
2020-09-07 10:14:252209browse

Yii url hide index.php method: first add the ".htaccess" file in the web root directory; then modify the configuration "config/web.php"; finally change "AllowOverride None" to "AllowOverride All" ;" That's it.

yii url hide index.php

Recommended: "PHP Video Tutorial" "yii Tutorial"

The Yii framework enables URL beautification and hides index.php [2.0 version]

url beautification

Purpose: http://localtest/yii/web/index.php?r= hello/index

is beautified into: http://localtest/yii/web/hello/index

Here I used wampserver to create a new localtest site (click here for details) , and rename the basic folder of yii to yii.

Comparing the two addresses above, we actually hide index.php?r=.

There are two steps here:

1. Add the .htaccess file

Add the .htaccess file in the web root directory, the content is:

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

Unable To create .htaccess directly, you can first create a txt file, and then save it as..., save the file name as .htaccess, and select all files as the saving type.

2. Configure config/web.php. Add this item to the components array in config/web.php:

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

At this time, you can change the index.php?r in the URL =Delete, if a 404 error occurs, you can check the server configuration. I am using apache integrated in phpstudy. You need to check the configuration

conf\httpd.conf and enable the mod_rewrite module of apache

Remove the "#" symbol before LoadModule rewrite_module modules/mod_rewrite.so;

Then modify the AllowOverride of apache

Change AllowOverride None to AllowOverride All;

Because I am The site is configured in conf\extra\httpd-vhosts.conf, so it needs to be synchronized to httpd-vhosts.conf to change the AllowOverride None of the corresponding site to AllowOverride All;

At this point, I can use http: //localtest/yii/web/hello/index

to access http://localtest/yii/web/index.php?r=hello/index

The above is the detailed content of yii url hide index.php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn