场景说明

现有配置是ThinkPHP 通用伪静态(根目录部署)项目放在子目录,例如:shturl.cc/SnVrcsF5f,网站根目录子文件夹 sub

方案 1:Nginx 子目录伪静态(推荐,无需改动原有根配置)

原始代码问题

原来 rewrite ^(.*)$ /index.php?s=$1 last;绝对路径 /index.php,子目录下会 404,要改成子目录路径。
完整示例(子目录名称:sub

location ~* (runtime|application)/{

    return 403;

}


location /sub/ {

    if (!-e $request_filename){

        rewrite ^/sub/(.*)$  /sub/index.php?s=$1  last;

    }

}


# 根目录原有配置保留不变

location / {

    if (!-e $request_filename){

        rewrite  ^(.*)$  /index.php?s=$1  last;

    }

}



请注意sub是你的子目录名称。

⚠️ 关键点:
  1. location 匹配 /sub/(末尾斜杠必须带上)

  2. rewrite 正则 ^/sub/(.*)$

  3. 目标地址 /sub/index.php?s=$1 带上子目录

  4. 删掉多余 break;,last 模式下 break 无效

方案 2:多个子目录统一写法(如有 sub1、sub2)

location ~* (runtime|application)/{

    return 403;

}


# 子目录sub

location /sub/ {

    if (!-e $request_filename){

        rewrite ^/sub/(.*)$  /sub/index.php?s=$1  last;

    }

}

# 子目录admin

location /admin/ {

    if (!-e $request_filename){

        rewrite ^/admin/(.*)$  /admin/index.php?s=$1  last;

    }

}


location / {

    if (!-e $request_filename){

        rewrite  ^(.*)$  /index.php?s=$1  last;

    }

}


以上方法测试可用


未经允许不得转载! 作者:【客服】小秋,转载或复制请以超链接形式并注明出处学习吧_一个不错的学习网站

原文地址:《易优CMS网站子目录Nginx环境的子目录伪静态写法教程》发布于:2026-08-01 21:33:42

加载中~