I’d like to convert an IIS rewrite into Nginx rewrite syntax. I am looking for a way to rewrite a URL like /leaderboard
into /pages.php?page=leaderboard
. My IIS Rewrite Rule is:
<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}" />
Current NGiNX Conf:
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; } }
Advertisement
Answer
server { rewrite ^/([^/]+)/?$ /pages.php?page=$1 break; ... }
Matches (see tests):
❌ / ✅ /foo ✅ /foo/ ❌ /foo/bar ❌ /foo/bar/baz