尝试:
RewriteEngine On
# Match against the request instead of the URI
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^&\ ]+)&?([^\ ]*)
RewriteRule ^$ /%1?%2 [L,R=301]
这需要 URI 的 likehttp://example.com/?path/to/file.txt并将浏览器重定向到http://example.com/path/to/file.txt. 浏览器将改为在地址栏中显示该 URL。当然,这是假设如果有人真的访问了那个 URL,那么除了 404 之外还有其他东西可以提供。
编辑
要在内部将非查询字符串 URL 映射到具有查询字符串的 URL:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?$1 [L]