# 仅对 /bbs 目录及子目录生效的伪静态规则
location /bbs/ {
# 保留原始请求路径的变量(避免嵌套location干扰)
set $real_request $request_filename;
# 规则1:如果请求路径下存在 index.html,重写到该文件
if (-f $real_request/index.html) {
rewrite (.*) $1/index.html break;
}
# 规则2:如果请求路径下存在 index.php,重写到该文件
if (-f $real_request/index.php) {
rewrite (.*) $1/index.php;
}
# 规则3:如果请求的文件不存在,重写到 bbs 目录下的 index.php(核心修改)
if (!-f $real_request) {
rewrite (.*) /bbs/index.php last;
}
# 可选:如果 bbs 目录下有 PHP 文件需要解析,添加 PHP 处理规则
location ~ /bbs/.*\.php$ {
fastcgi_pass 127.0.0.1:9000; # 根据你的 PHP-FPM 端口调整(常见9000/9001)
fastcgi_index index.php;
# 确保 PHP 脚本路径正确($document_root 是网站根目录)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
如果不配置 #可选,要删除上面的
# 可选:如果 bbs 目录下有 PHP 文件需要解析,添加 PHP 处理规则
location ~ /bbs/.*\.php$ {
fastcgi_pass 127.0.0.1:9000; # 根据你的 PHP-FPM 端口调整(常见9000/9001)
fastcgi_index index.php;
# 确保 PHP 脚本路径正确($document_root 是网站根目录)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
测试网址https://www.jiedublog.com/bbs/
需要的是Apache 环境下适配 bbs 目录的伪静态规则(mod_rewrite 模块),复制以下代码:
# 开启 URL 重写功能
RewriteEngine On
# 设定重写的基准路径为 /bbs/(核心:限定规则作用域)
RewriteBase /bbs/
# 条件1:请求的路径不是一个真实存在的文件
RewriteCond %{REQUEST_FILENAME} !-f
# 条件2:请求的路径不是一个真实存在的目录
RewriteCond %{REQUEST_FILENAME} !-d
# 规则:将所有不符合上述条件的请求重写到 bbs 目录下的 index.php
# [L] 表示这是最后一条规则,停止后续匹配
RewriteRule ^(.*)$ index.php [L]
bbs 目录的 IIS 伪静态规则(web.config):