去除yii2.0 URL地址中的index.php以及美化URL的方法

1,服务器配置(apache)

代码以配置虚拟主机为例,如果不是虚拟主机的环境,参考Directory中的参数配置即可.配置文件:/etc/httpd/conf/httpd.cnf

<VirtualHost *:83>
    DocumentRoot /var/www/html/website/
    <Directory "/var/www/html/website/">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog logs/website-error_log
    CustomLog logs/website-access_log common
</VirtualHost>


2,美化URL

配置文件:根目录/应用目录/config/main.php

components数组中添加如下代码:

//URL美化配置
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'suffix' => '.html',
            'rules' => [
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
            ],
        ],

3,去除index.php

在应用入口处添加伪静态配置文件.htaccess,内容如下:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php


打开站点,查看url地址,大功告成!

猜你喜欢

转载自blog.csdn.net/xfcy1990/article/details/90173536
今日推荐