在作thinkphp的開發項目中,遇到一個需求:要求讓網站的連接,必須以 .html結尾。php
緣由:在thinkphp開發的項目中,使用僞靜態,路由格式:xxx.com/xxx/2.html ,可是後面的 .html 是默認的,經過訪問 xxx.com/xxx/2 也可以訪問。html
如今要求只可以經過:xxx.com/xxx/2.html 訪問,也就是結尾必須有 .html nginx
解決方式:thinkphp
一、服務器 apache 或是是 nginx 進行配置重定向。apache
二、經過thinkphp的路由來解決服務器
咱們能夠經過一個方法來進行驗證當前路由是否帶有 .html 。app
具體操做:網站
第一步:配置路由url
':name/:id' => ['index/index/details',['before_behavior'=>'\app\index\behavior\UserCheck']],
第二步:自定義方法進行驗證spa
<?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; } } }