yii2中的漂亮url不工作

vwkv1x7d  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(159)

在web.php中我有这个

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
        ],
    ],

apache为这个文件夹配置像这个php.conf文件

<VirtualHost *:80>
AssignUserId alexzander alexzander
ServerName localhost

DocumentRoot /home/alexzander/Dropbox/study/3year/2/php/
<Directory /home/alexzander/Dropbox/study/3year/2/php/>
    # use mod_rewrite for pretty URL support
    RewriteEngine on

    # If a directory or a file exists, use the request directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Otherwise forward the request to index.php
    RewriteRule . index.php

    Order allow,deny
    Allow from all
    Require all granted
    AllowOverride All
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

当我试图访问localhost/basic/images/list
我得到The requested URL /index.php was not found on this server.
当我在basic之后添加index.php时,它可以工作localhost/basic/index.php/images/list
我如何让漂亮的网址工作?我认为appache重写规则是不工作,但不知道为什么。
在错误日志中

[Thu Jun 02 20:46:13.811518 2016] [:error] [pid 25046] [client 127.0.0.1:52450] script '/home/alexzander/Dropbox/study/3year/2/php/index.php' not found or unable to stat, referer: http://localhost/basic/index.php/images/list

但这是因为文档根在apache中,在/home/alexzander/Dropbox/study/3year/2/php/中查找,我认为这是可以的

apache2ctl -M

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_itk_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php5_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)
uurity8g

uurity8g1#

URL管理器..

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
        ],
    ],

对于Apache服务器

然后添加名为.htaccess的新文件或编辑项目文件夹中的现有文件(未处于protected状态)。

然后将下面的代码添加到您的.htaccess文件

RewriteEngine on

# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php

RewriteRule . index.php

这是为我工作,并删除了index.php.

用于Nginx服务器

检查Nginx配置中的服务器块,如下所示:

server{
    listen      8082;
    server_name yii2.dev;
    access_log logs/yii2.access.log;
    error_log logs/yii2.error.log error;
    root /home/admin/web/nginx/html/basic/web/;
    location / {
            index  index.html index.php;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
    }
}

您可以在这里阅读Nginx服务器的Yii 2官方指南。

相关问题