一种是官方给的

if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php$1 last;
    }

因为宝塔waf新功能要求,所以又有了另一种伪静态

location / {
    # 尝试访问 URI,如果文件或目录不存在,则将请求重写到 index.php
    try_files $uri $uri/ /index.php$is_args$args;
}

# 处理 Typecho 后台路径,一般 /admin 是后台路径的起始部分
location /admin {
    # 直接交给 index.php 处理,以免 rewrite 规则影响后台
    try_files $uri $uri/ /index.php$is_args$args;
}

说明

  • location /: 指定应用伪静态规则的路径,在这里是根路径。
  • try_files $uri /index.php$is_args$args:

    • $uri 表示用户请求的 URI 文件路径。
    • try_files 尝试检查 $uri 对应的文件是否存在。
    • 如果文件不存在,则请求会被传递到 /index.php,并附带请求参数。
    • $is_args 是一个 Nginx 内置变量,用于在参数存在时插入问号 ?。
    • $args 表示请求的参数,即原始的查询字符串。
最后修改:2024 年 12 月 21 日
如果觉得我的文章对你有用,请随意赞赏