RewriteCond %{REQUEST_FILENAME} !-f含义及Nginx等价配置转换求助
What does RewriteCond %{REQUEST_FILENAME} !-f mean?
This is an Apache rewrite condition that checks if the requested path does not correspond to an existing regular file on the server filesystem:
!negates the check (so "not")-ftests whether the request maps to a valid, regular file (as opposed to a directory, symlink, etc.)
Combined with the second condition !-d, these rules tell Apache to skip the rewrite if the request targets a real file or directory—only rewriting when the path doesn't exist on disk.
Why the online tool's Nginx config fails with ?id=10
The generated try_files rule replaces the original query string when specifying /index.php?rt=$1. Unlike Apache's QSA (Query String Append) flag, try_files doesn't automatically preserve existing query parameters. This is why $_GET['id'] is null—those parameters are discarded instead of being appended to the new query string.
Equivalent Nginx Config (matches Apache's behavior exactly)
To replicate the full functionality of your Apache rules (including preserving query parameters and escaping special characters), use this configuration:
# Serve existing files and directories directly location / { try_files $uri $uri/ @fallback; } # Rewrite non-existent paths to index.php with preserved query params location @fallback { # Capture the path after the root, escape special characters (matches Apache's B flag) # Use QSA to append original query parameters (matches Apache's QSA flag) rewrite ^/(.*)$ /index.php?rt=$escape_uri($1) QSA last; }
Key details matching Apache's behavior:
try_files $uri $uri/ @fallback: Skips rewrite for existing files ($uri) and directories ($uri/), mirroring Apache's!-fand!-dconditions.$escape_uri($1): URL-escapes the captured path, replicating Apache'sBflag to handle special characters (spaces, ampersands, etc.) correctly.QSAflag: Appends the original query string (e.g.,?id=10) to the newrt=$1parameter, ensuring all$_GETvariables are preserved.lastflag: Stops processing further rewrite rules for this request, matching Apache'sLflag.
If you're using an older Nginx version without escape_uri() support, use $uri (already escaped) and strip the leading slash:
location @fallback { rewrite ^.*$ /index.php?rt=$substr($uri, 1) QSA last; }
内容的提问来源于stack exchange,提问作者Martin AJ

