【问题标题】:Add a htacess redirect to a single Yii2 controller将 htaccess 重定向添加到单个 Yii2 控制器
【发布时间】:2017-03-27 21:22:14
【问题描述】:

我在 Yii2 PHP 站点上有两个控制器。第一个是站点控制器。第二个是博客控制器。我有这个现有的重定向设置。

Options +FollowSymLinks
IndexIgnore */*

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 ^.*$ frontend/web/index.php

这很好用,网址重定向如下:

/site/about 重定向到/frontend/web/index.php/site/about

我现在正在尝试设置行为来重定向我的博客帖子。

目前,这些帖子在/blog/view?slug=SLUG 可见。我希望它们在/blog/SLUG 上可见。我想让/blog 重定向到/blog/index

我不擅长正则表达式和 htaccess 重写。我尝试了任意数量的衍生产品,但无法正确重定向网址。

/blog/test 应该重定向到 /frontend/web/index.php/blog/view?slug=test/blog//blog 应该重定向到 /frontend/web/index.php/blog/index

有人可以帮忙吗?我最近(失败)的尝试是:

Options +FollowSymLinks
IndexIgnore */*

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^blog/(.*)$ frontend/web/index.php/blog/view?slug=%1

# otherwise forward it to index.php
RewriteRule ^.*$ frontend/web/index.php

如果任何有 Yii2 经验的人想插话,我实际上也可能误解了我当前的重定向。

【问题讨论】:

    标签: php .htaccess redirect yii yii2


    【解决方案1】:

    你为什么不在 Yii2 本身中使用规则。它们可以在配置中设置 prettyURL 像

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

    ],

    这比改变你的 htaccess 文件要好,因为如果 Yii2 有一个特性,那为什么不使用它呢?希望我能解决您的疑问,如果没有,请告诉我。

    【讨论】:

    • 太棒了!我为 api 组件使用规则,不完全确定为什么我没有想到前端:P
    猜你喜欢
    • 2012-07-16
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2020-08-21
    • 2010-12-15
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多