针对某些thinkphp站点在百度app访问异常的404页面

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>页面未找到</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                text-align: center;
                margin: 0;
                padding: 20px;
                background-color: #f5f5f5;
                display: flex;
                justify-content: center;
                align-items: center;
                min-height: 100vh;
            }
            .container {
                background-color: white;
                border-radius: 10px;
                padding: 20px;
                max-width: 500px;
                margin: 0 auto;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                animation: fadeInUp 0.8s ease-out;
            }
            @keyframes fadeInUp {
                from {
                    opacity: 0;
                    transform: translateY(20px);
                }
                to {
                    opacity: 1;
                    transform: translateY(0);
                }
            }
            h1 {
                color: #333;
                margin-bottom: 20px;
                animation: bounce 1s infinite alternate;
            }
            @keyframes bounce {
                from {
                    transform: translateY(0);
                }
                to {
                    transform: translateY(-10px);
                }
            }
            p {
                color: #666;
                line-height: 1.6;
                margin-bottom: 30px;
            }
            button {
                background-color: #4285f4;
                color: white;
                border: none;
                padding: 12px 24px;
                font-size: 16px;
                border-radius: 5px;
                cursor: pointer;
                transition: background-color 0.3s, transform 0.2s;
            }
            button:hover {
                background-color: #3367d6;
                transform: scale(1.05);
            }
            .toast {
                position: fixed;
                bottom: 20px;
                left: 50%;
                transform: translateX(-50%);
                background-color: rgba(0, 0, 0, 0.8);
                color: white;
                padding: 10px 20px;
                border-radius: 5px;
                opacity: 0;
                transition: opacity 0.3s;
                z-index: 1000;
            }
        </style>
    </head>
    <body>
        <div id="toast">链接已复制,请手动粘贴到其他浏览器中打开访问,您受累了!</div>
    
        <script>
            // 检测是否是百度APP访问
            function isBaiduApp() {
                const userAgent = navigator.userAgent.toLowerCase();
                const urlParams = new URLSearchParams(window.location.search);
    
                // 特征1:User-Agent包含 baiduboxapp
                const isBaiduByUA = userAgent.includes('baiduboxapp');
    
                // 特征2:来自百度APP的请求可能携带特定参数(如 from=baiduapp)
                const isBaiduByParam = urlParams.get('from') === 'baiduapp';
    
                // 特征3:空User-Agent且访问根路径(根据日志补充逻辑)
                const isBaiduEmptyUA = !userAgent && window.location.pathname === '/';
    
                return isBaiduByUA || isBaiduByParam || isBaiduEmptyUA;
            }
    
            // 根据是否是百度APP访问,显示不同的提示页面
            if (isBaiduApp()) {
                document.body.innerHTML += `
                    <div>
                        <h1>嘿!注意啦!</h1>
                        <p>当前页面在百度APP里有点“小脾气”,打不开哟! 快用手机默认浏览器来试试,它会乖乖听话哒!</p>
                        <button onclick="copyLink()">复制链接用默认浏览器访问</button>
                    </div>
                `;
            } else {
                document.body.innerHTML += `
                    <div>
                        <h1>哎呀!</h1>
                        <p>不要伤心,可能是网址错了呢,重新核对一下吧。</p>
                    </div>
                `;
            }
    
            // 复制链接功能
            function copyLink() {
                const textArea = document.createElement('textarea');
                textArea.value = window.location.href;
                document.body.appendChild(textArea);
                textArea.select();
                document.execCommand('copy');
                document.body.removeChild(textArea);
                showTooltip();
            }
    
            // 显示提示框
            function showTooltip() {
                const toast = document.getElementById('toast');
                toast.style.opacity = 1;
                setTimeout(() => {
                    toast.style.opacity = 0;
                }, 3000);
            }
        </script>
    </body>
    </html>

    php代码:

    $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
    $requestPath = $_SERVER['REQUEST_URI'] ?? '';
    
    // ===== 全局拦截百度APP(所有页面生效)=====
    $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
    $requestUri = $_SERVER['REQUEST_URI'] ?? '';
    
    // 特征1:User-Agent包含 baiduboxapp
    $isBaiduByUA = stripos($userAgent, 'baiduboxapp') !== false;
    
    // 特征2:来自百度APP的请求可能携带特定参数(如 from=baiduapp)
    $isBaiduByParam = isset($_GET['from']) && $_GET['from'] === 'baiduapp';
    
    // 特征3:空User-Agent且访问根路径(根据日志补充逻辑)
    $isBaiduEmptyUA = empty($userAgent) && $requestPath === '/';
    
    if ($isBaiduByUA || $isBaiduByParam || $isBaiduEmptyUA) {
        header("HTTP/1.1 200 OK");
        echo <<<HTML
        <!DOCTYPE html>
        <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style> body { font-family: Arial, sans-serif; text-align: center; margin: 0; padding: 20px; background-color: #f5f5f5; display: flex; justify-content: center; align-items: center; min-height: 100vh; } .container { background-color: white; border-radius: 10px; padding: 20px; max-width: 500px; margin: 0 auto; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); animation: fadeInUp 0.8s ease-out; } @keyframes fadeInUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } h1 { color: #333; margin-bottom: 20px; animation: bounce 1s infinite alternate; } @keyframes bounce { from { transform: translateY(0); } to { transform: translateY(-10px); } } p { color: #666; line-height: 1.6; margin-bottom: 30px; } button { background-color: #4285f4; color: white; border: none; padding: 12px 24px; font-size: 16px; border-radius: 5px; cursor: pointer; transition: background-color 0.3s, transform 0.2s; } button:hover { background-color: #3367d6; transform: scale(1.05); } .toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background-color: rgba(0, 0, 0, 0.8); color: white; padding: 10px 20px; border-radius: 5px; opacity: 0; transition: opacity 0.3s; } </style>
    </head>
    <body>
        <div>
            <h1>嘿!注意啦!</h1>
            <p>当前页面在百度APP里有点“小脾气”,打不开哟! 快用手机默认浏览器来试试,它会乖乖听话哒!</p>
            <button onclick="copyLink()">复制链接用默认浏览器访问</button>
        </div>
        <div id="toast">链接已复制,请手动粘贴到其他浏览器中打开访问,您受累了!</div>
       <script> function openInBrowser() { window.open(window.location.href, '_blank'); } function copyLink() { const textArea = document.createElement('textarea'); textArea.value = window.location.href; document.body.appendChild(textArea); textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); const toast = document.getElementById('toast'); toast.style.opacity = 1; setTimeout(() => { toast.style.opacity = 0; }, 3000); } </script>
    </body>
    </html>    
    HTML;
        exit;
    }