URL 在 Nginx 中重写
URL with Rewrite in Nginx
我希望 URL 重写后看起来干净漂亮。我在使用 Apache 时能够做到这一点,但我在服务器上安装了 Nginx。所以我不使用 .htaccess
.
在使用 Apache 时,我是这样使用它的:
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ .php [L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteCond %{REQUEST_URI} !.(css|gif|ico|jpg|js|png|swf|txt)$
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)?$ index.php?cmd=&scd= [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?cmd= [L,QSA]
</IfModule>
示例 URL 如下所示:
https://example.com/item/1234/bund123
没有mod_rewrite:
https://example.com/index.php?item?id=1234&unt=bund123
我尝试在 Nginx 上执行此操作,但出现 404 错误。这可能是什么原因?
server {
listen *:80;
listen [::]:80;
listen *:443 ssl http2;
server_name example.com www.example.com;
root /var/www/example.com/web/;
rewrite ^([^/]+)/([^/]+)?$ /index.php?cmd=&scd= last;
rewrite ^([^/]+)?$ /index.php?cmd= last;
....
}
我哪里做错了?
关于404错误的详细信息可以在Nginx错误日志中找到。据推测,index.php不存在。
可以使用 redirect
和 Curl 快速测试重写规则:
# /item/cmd/scd
rewrite ^/item/([^/]+)/([^/]+) /index.php?cmd=&scd= redirect;
# /item/cmd
rewrite ^/item/([^/]+) /index.php?cmd= redirect;
$ curl -I https://example.com/item/CMD/SCD
HTTP/1.1 302 Moved Temporarily
Location: https://example.com/index.php?cmd=CMD&scd=SCD
$ curl -I https://example.com/item/CMD
HTTP/1.1 302 Moved Temporarily
Location: https://example.com/index.php?cmd=CMD
当重写工作正常时,将 redirect
更改为 break
:
rewrite ^/item/([^/]+)/([^/]+) /index.php?cmd=&scd= break;
rewrite ^/item/([^/]+) /index.php?cmd= break;
我希望 URL 重写后看起来干净漂亮。我在使用 Apache 时能够做到这一点,但我在服务器上安装了 Nginx。所以我不使用 .htaccess
.
在使用 Apache 时,我是这样使用它的:
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ .php [L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteCond %{REQUEST_URI} !.(css|gif|ico|jpg|js|png|swf|txt)$
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)?$ index.php?cmd=&scd= [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?cmd= [L,QSA]
</IfModule>
示例 URL 如下所示:
https://example.com/item/1234/bund123
没有mod_rewrite:
https://example.com/index.php?item?id=1234&unt=bund123
我尝试在 Nginx 上执行此操作,但出现 404 错误。这可能是什么原因?
server {
listen *:80;
listen [::]:80;
listen *:443 ssl http2;
server_name example.com www.example.com;
root /var/www/example.com/web/;
rewrite ^([^/]+)/([^/]+)?$ /index.php?cmd=&scd= last;
rewrite ^([^/]+)?$ /index.php?cmd= last;
....
}
我哪里做错了?
关于404错误的详细信息可以在Nginx错误日志中找到。据推测,index.php不存在。
可以使用 redirect
和 Curl 快速测试重写规则:
# /item/cmd/scd
rewrite ^/item/([^/]+)/([^/]+) /index.php?cmd=&scd= redirect;
# /item/cmd
rewrite ^/item/([^/]+) /index.php?cmd= redirect;
$ curl -I https://example.com/item/CMD/SCD
HTTP/1.1 302 Moved Temporarily
Location: https://example.com/index.php?cmd=CMD&scd=SCD
$ curl -I https://example.com/item/CMD
HTTP/1.1 302 Moved Temporarily
Location: https://example.com/index.php?cmd=CMD
当重写工作正常时,将 redirect
更改为 break
:
rewrite ^/item/([^/]+)/([^/]+) /index.php?cmd=&scd= break;
rewrite ^/item/([^/]+) /index.php?cmd= break;