php - Yii2 自定义 url 管理。出现 400 错误

标签 php apache .htaccess yii-components yii2

嗯,PHP 时代。

我的客户希望我使用 Yii2 作为他项目的框架。

我已经启动并运行了。没问题。我通过 composer 使用了高级模板。

将我的网站根目录设置为/frontend/web 等

现在,我想使用这种 url 格式

website.com/messages/或 website.com/messages/tom...等

现在设置方式显示 website.com/index.php?r=messages/index...

我找到了这个文档...

https://github.com/yiisoft/yii2/blob/master/docs/guide/url.md

但我似乎无法弄清楚。

这是我的步骤...

我将我的 apache 服务器配置为指向/usr/www/payroll/frontend/web/

我在我的网络文件夹中添加了一个包含此内容的 .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

我还按照说明添加了组件“urlManager”。它似乎捕获了请求并对其进行了修改。

'components'=>[
    'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => 'false'
        ],
],

例如,如果我输入 website.com,您可以看到它向 url 添加了/site/index。 (没有激活 url 组件,它只是添加/index.php?site/index)

所以,显然对 url 进行了修改(通过 UrlManager),但我收到 404 错误

我的想法已经用完了。我是 Php、Apache 和 Yii2 的新手。任何帮助,非常感谢。

谢谢

最佳答案

要使漂亮的 URL 在 Yii 2.0 上工作,您需要两件事:

1: 编辑 /frontend/config/main.php(或您的情况下的适当主配置)并添加:

'components'=>[
        'urlManager' => [
                'class' => 'yii\web\UrlManager',
                'enablePrettyUrl' => true,
                'showScriptName' => false,
            ],
    ],

2: 在YOUR WEB ROOT 文件夹中添加一个.htaccess 文件。 在 yii 2.0 advanced 中,这是 NOT 项目根目录,而是: /前端/网络

.htaccess 示例:

RewriteEngine on

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

RewriteRule . index.php

关于php - Yii2 自定义 url 管理。出现 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22993617/

相关文章:

php - 无法让 WordPress 显示多站点中的所有类别

php - 没有来自 docker 容器的 php-fpm 错误日志

java - 让 Spring MVC 应用程序在 Apache 下运行

.htaccess - htaccess 重定向除索引页之外的所有内容

wordpress - .htaccess 和 wordpress 301 重定向不在批准的 IP 地址列表中的每个人?

php - 关于选择选项值

php - 在 PHP 中使用 ajax 预填充相应国家/地区的州详细信息

PHP+GD : imagecopymerge not retaining PNG transparencies

php - 如何在浏览器中显示大约 1000 张每个 30 kb 的图像?

php - .htaccess RewriteRule 排除文件夹