duanpu5048 2017-10-17 08:45
浏览 216
已采纳

Yii2:如何使用301重定向旧URL?

I have some URLs from the old version of my site that I want to redirect to their new ones in Yii2 due to SEO purposes, eg. /about-us.php to /about. How do I do that?

I can't use .htaccess, and I can't use urlManager rules because HTTP response status 301 needs to be set.

I tried the following in a bootstrap class but even though the code executes I still just get a 404 when going to the old URL:

if (preg_match("|^/about.php|", $_SERVER['REQUEST_URI'])) {
    Yii::$app->response->redirect('/about', 301)->send();
    return;
}
  • 写回答

2条回答 默认 最新

  • dongtang5776 2017-10-17 08:52
    关注

    Just found the answer myself. My bootstrap attempt was not so bad, I just needed to add Yii::$app->end():

    if (preg_match("|^/about-us.php|", $_SERVER['REQUEST_URI'])) {
        Yii::$app->response->redirect('/about', 301)->send();
        Yii::$app->end();
        return;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作