.htaccess:使用未知子文件夹重定向

时间:2016-01-05 13:28:26

标签: .htaccess redirect

我想使用mod_rewrite重定向所有以已知名称结尾的URI,但子文件夹未知。像这样:

自:

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => ['signup', 'captcha'],
                    'allow' => true,
                    'roles' => ['?'],
                ],
                [
                    'actions' => ['logout'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
    ];
}

要:

mymixin(myval = 0)
  width (myval)%

.myclass
  mymixin(20)

example.com/random1/random2/my-text example.com/my-text/ 未知。我尝试了random1和许多变种,但我无法让它发挥作用。

有什么想法吗?

更新:现有.htaccess文件:

random2

1 个答案:

答案 0 :(得分:1)

要使用.htaccess中的mod_rewrite重定向以my-text结尾的所有网址,请尝试以下操作:

RewriteEngine On
RewriteRule .+/(my-text)$ /$1/ [R=302,L]

请注意,源URL不是以斜杠结尾,而是以目标为结尾(根据您的示例)。

如果您确定它正常工作,请将302(临时)重定向更改为301(永久) - 假设这应该是永久重定向?

请注意,外部重定向通常应该在.htaccess文件中重写请求的任何现有指令之前。 (因此,上面的内容应该在您现有的.htaccess文件中的RewriteBase指令之后立即执行。RewriteEngine只需要出现一次。)