# 开启重写引擎 RewriteEngine On # 对应:index index.html index.htm index.php; DirectoryIndex index.html index.htm index.php # 1. 如果请求的是真实存在的目录或文件,直接跳过重写 RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L] # 2. 对应:location ~ \.php$ { try_files $uri =404; } # 保护机制:如果请求以 .php 结尾但文件不存在,直接返回 404 (这也能防止下方的规则产生死循环) RewriteCond %{REQUEST_URI} \.php$ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ - [R=404,L] # 3. 对应:try_files $uri $uri/ @extensionless-php; 以及 rewrite ^(.*)$ $1.php last; # 将所有找不到的请求追加 .php 后缀进行内部重定向 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ $1.php [L]