[...]" to the components array in config/web.php; 3. Modify apache."/> [...]" to the components array in config/web.php; 3. Modify apache.">

Home  >  Article  >  PHP Framework  >  How to hide .php suffix in yii url

How to hide .php suffix in yii url

藏色散人
藏色散人Original
2022-01-04 09:24:592434browse

Yii How to hide the .php suffix in url: 1. Add the .htaccess file; 2. Add "'urlManager' => [...]" to the components array in config/web.php; 3. Just modify apache.

How to hide .php suffix in yii url

#The operating environment of this article: Windows 7 system, version yii2.0, Dell G3 computer.

yii url How to hide the .php suffix?

Yii framework enables URL beautification, hiding index.php [2.0 version]

url beautification:

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

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

Here I used wampserver to create a new localtest site (click here for details), and renamed 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 visit http://localtest/yii/web/index.php?r=hello/index

Recommended: " yii tutorial

The above is the detailed content of How to hide .php suffix in yii url. 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