我想将 IIS 重写转换为 Nginx 重写语法。我正在寻找一种将 /leaderboard
等 URL 重写为 /pages.php?page=leaderboard
的方法。我的 IIS 重写规则是:
<match url="^([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="pages.php?page={R:1}" />
当前 NGiNX session :
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
server_name example.com www.example.com;
root /var/www/example.com/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
最佳答案
server {
rewrite ^/([^/]+)/?$ /pages.php?page=$1 break;
...
}
匹配 ( see tests ):
❌ /
✅ /foo
✅ /foo/
❌ /foo/bar
❌ /foo/bar/baz
关于php - 将 IIS 重写转换为 Nginx 重写语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70340393/