【问题标题】:Convert IIS rewrite to Nginx rewrite syntax?将 IIS 重写转换为 Nginx 重写语法?
【发布时间】:2022-01-17 06:20:12
【问题描述】:

我想将 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 会议:

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;
    }
}

【问题讨论】:

  • 当用户导航到example.com/leaderboard时应该显示什么文件?
  • 那就是 pages.php?page=leaderboard
  • 谢谢。我反转并更正了原始问题中的源/目标 URL。

标签: php regex nginx web iis


【解决方案1】:
server {
  rewrite ^/([^/]+)/?$ /pages.php?page=$1 break;
  ...
}

匹配项 (see tests):

❌  /
✅  /foo
✅  /foo/
❌  /foo/bar
❌  /foo/bar/baz

【讨论】:

    猜你喜欢
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 2013-12-01
    • 2015-11-11
    • 1970-01-01
    相关资源
    最近更新 更多