yii2 中的漂亮网址不起作用 [英] pretty url in yii2 not working

查看:60
本文介绍了yii2 中的漂亮网址不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 web.php 中我有这个

In web.php i have this

'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文件

apache for this folder configured like this php.conf file

<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

我明白了在此服务器上找不到请求的 URL/index.php.

当我在基本之后添加 index.php 时,它可以工作localhost/basic/index.php/images/list

when I add index.php after basic it works localhost/basic/index.php/images/list

如何让漂亮的网址正常工作?我认为 appache 重写规则不起作用,但不知道为什么.

How do i get pretty url working? I think appache rewrite rule is not working, but don't know why.

在错误日志中

[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/ 我认为没关系

but this because document root in apache looking inside /home/alexzander/Dropbox/study/3year/2/php/ I think that's ok

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)

推荐答案

UrlManager..

UrlManager..

'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 的新文件或编辑项目文件夹中的现有文件(未受保护).

Then add a new file with name .htaccess or edit an existing file in your project folder (not in protected).

然后将此代码添加到您的 .htaccess 文件中

Then add this code below to your .htaccess file

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.

This is working for me and removed the index.php.

对于 Nginx 服务器

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

Check the server block in Nginx configuration as following:

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;
            }
    }
}

您可以阅读 Yii 2 for nginx 服务器的官方指南 这里.

You can read the official guide of Yii 2 for nginx server here.

这篇关于yii2 中的漂亮网址不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆