隐藏式跳转(也称为 URL 重写或内部重定向)是指在服务器内部完成跳转,浏览器地址栏不会发生变化。以下是几种在 Nginx 中实现隐藏式跳转的方法:
location /old-url {
rewrite ^/old-url(.*)$ /new-url$1 last;
}
last
标志表示完成重写后继续处理其他 location 块$1
表示捕获的路径部分location /old-url {
try_files $uri $uri/ /new-url;
}
location /old-url {
alias /path/to/new-url;
}
location /old-url {
proxy_pass http://backend/new-url;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
rewrite
时注意正则表达式的正确性nginx -t && nginx -s reload
)可以在配置中添加日志来调试重写规则:
rewrite_log on;
error_log /var/log/nginx/error.log notice;
这些方法可以根据您的具体需求选择使用,实现隐藏式的 URL 跳转功能。