【发布时间】:2022-01-25 04:29:15
【问题描述】:
我希望 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 ^(.*)$ $1.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=$1&scd=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?cmd=$1 [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=$1&scd=$2 last;
rewrite ^([^/]+)?$ /index.php?cmd=$1 last;
....
}
我哪里做错了?
【问题讨论】:
标签: .htaccess nginx mod-rewrite url-rewriting