[英]how do I rewrite a URL in nginx to an updated location?
I have the following URLs: 我有以下网址:
1) http://example.com/downloads/
2) http://example.com/downloads/widgets
3) http://example.com/downloads/gadgets
These URLs need to be redirected rewritten to the following: 这些URL需要重定向到以下内容:
1) http://example.com/products/
2 & 3 & etc) http://example.com/products/thingies
I'm currently trying the following nginx code: 我目前正在尝试以下nginx代码:
location ~* ^/downloads/ {
rewrite ^/downloads/$ /products break;
rewrite ^/downloads/(.*)$ /products/thingies break;
}
It's almost working, however my site's document root is /var/www/example.com/public
. 差不多可以了 ,但是我网站的文档根目录是/var/www/example.com/public
。 So after processing the rewrite rules, nginx tries to literally serve /var/www/example.com/public/products/
, whereas I want it to just rewrite to http://example.com/products/
(which then proxies to PHP and so on). 因此,在处理了重写规则之后,nginx尝试从字面上服务/var/www/example.com/public/products/
,而我希望它只是重写为http://example.com/products/
(然后将其代理为PHP)等等)。
Where am I failing? 我在哪里失败? Is there a different, better way to accomplish this? 有另一种更好的方法可以做到这一点吗?
Thank you for any help. 感谢您的任何帮助。
I got it to work by using the following rules: 我通过使用以下规则使其工作:
rewrite ^/downloads/?$ $scheme://$host/tools last; 最后重写^ / downloads /?$ $ scheme:// $ host / tools;
location ~* ^/downloads/ {
rewrite ^/downloads/?$ $scheme://$host/products last;
rewrite ^/downloads/(.*)$ $scheme://$host/products/thingies last;
}
IS this the proper way of doing it in nginx? 这是在nginx中做的正确方法吗? I haven't seen this rewrite rule format anywhere while researching this. 在研究此格式时,我从未在任何地方看到此重写规则格式。 It somehow seems odd. 这似乎有点奇怪。
Your update redirects, not rewrites. 您的更新重定向,而不是重写。
Here is how I would do: 这是我的做法:
location /downloads/ {
rewrite ^ /products/thingies;
}
location = /downloads/ {
rewrite ^ /products/;
}
# uncomment if you need '/downloads' (without trailing slash) act as '/downloads/'
#location = /downloads {
# rewrite ^ /products/;
#}
The correct syntax appears to be: 正确的语法似乎是:
location ~* ^/downloads/ {
rewrite ^/downloads/?$ /products permanent;
rewrite ^/downloads/(.*)$ /products/thingies permanent;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.