thinkphp5---路由问题

在做thinkphp的开发项目中,遇到一个需求:要求让网站的链接,必须以 .html结尾。

原因:在thinkphp开发的项目中,使用伪静态,路由格式:xxx.com/xxx/2.html ,但是后面的 .html 是默认的,通过访问 xxx.com/xxx/2 也能够访问。

现在要求只能够通过:xxx.com/xxx/2.html 访问,也就是结尾必须有 .html 

解决方式:

1、服务器 apache 或是是 nginx 進行配置重定向。

2、通过thinkphp的路由来解决

我们可以通过一个方法来进行验证当前路由是否带有 .html 。

具体操作:

第一步:配置路由

':name/:id' => ['index/index/details',['before_behavior'=>'\app\index\behavior\UserCheck']],

第二步:自定义方法进行验证

<?php
namespace app\index\behavior;

class UserCheck{
    public function run(){
      $url = request()->url();
      if(!preg_match("/[\w\d]*.html$/",$url)){
        echo "不是以.html结尾的URL";
        // header("HTTP/1.1 404 Not Found");exit;
      }
    }
}

猜你喜欢

转载自www.cnblogs.com/e0yu/p/11271939.html
今日推荐