You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置.htaccess移除URL中?page=参数实现路径美化

实现伪静态的.htaccess配置方案

第一步:编写根目录.htaccess规则

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# 排除真实存在的文件、目录,避免静态资源被重写
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# 将路径参数传入index.php的page参数,QSA标记保留原有其他查询参数(如id等)
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>

第二步:调整原有index.php逻辑

你原有的index.php固定加载主题目录下的index.php,需要调整为根据page参数加载对应主题文件,同时增加安全校验避免路径遍历攻击:

<?php
    include('core/core.php');

    echo $_GET['id'] ?? ''; // 增加判空避免无id参数时报错

    $currentTheme = getPageData() -> theme_selected;
    // 读取page参数,首页默认值为index
    $pageName = $_GET['page'] ?? 'index';
    // 安全过滤,清除路径相关特殊字符,防止跨目录访问
    $pageName = str_replace(['/', '\\', '..'], '', $pageName);
    $targetFile = "jtq-theme/" . $currentTheme . "/" . $pageName . ".php";

    if(file_exists($targetFile)) {
        include($targetFile);
    } else {
        // 404逻辑,可自行替换为主题下的404页面
        http_response_code(404);
        include("jtq-theme/" . $currentTheme . "/404.php");
    }
?>

生效验证项

  • 确保Apache服务器已开启mod_rewrite扩展
  • 网站根目录的Apache配置中AllowOverride参数需设置为All,否则.htaccess规则不会生效
  • 主题目录下提前创建对应页面文件,比如about_us.php、contact.php等

内容的提问来源于stack exchange,提问作者Ahad Aman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 02:45:04