# 仅对 /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; }下面是根目录下应用的:
if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; }
如果是需要的是Apache 环境下适配 bbs 目录的伪静态规则(mod_rewrite 模块)以及bbs 目录的 IIS 伪静态规则(web.config),也可以下载这个文件查看。
![[!--sitename--]二维码](/skin/why80/images/WHY80_ewm.png)