使用负正则表达式重写nginx URL? [英] nginx URL rewrite using negative regex?
问题描述
我正在尝试将请求重定向到nginx中的https,除非它们是HOST/ANY_STRING_OF_CHARS/END_OF_URI的格式,例如:
I'm trying to redirect requests to https in nginx, unless it is of the form HOST/ANY_STRING_OF_CHARS/END_OF_URI, e.g.:
http://host.org/about #没有重定向
http://host.org/users/sign_in #重定向到这显然在Apache中有效,但我不知道这种爆炸是如何工作的(忽略它是否真的不起作用):
This apparently works in Apache, but I don't understand how the bang works (ignore if it doesn't really work):
RewriteRule !/([a-z]+)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
如何在nginx重写规则中执行此操作?这不符合我的期望:
How can I do this in a nginx rewrite rule? This is not working as I'd hoped:
rewrite !/([a-z]+)$ https://$server_name$request_uri redirect;
如果我的逻辑倒退了,这也不会重定向:
This doesn't do the redirect either, in case I had the logic backwards:
rewrite /([a-z]+)$ https://$server_name$request_uri redirect;
请帮助?
推荐答案
将永久重定向发送到客户端:
Sends a permanent redirect to the client:
server {
listen 80;
rewrite ^(/users/\w+)$ https://$host$1 permanent;
...
}
对于否定匹配,您可以使用:
for negative match you could use:
if ($request_uri !~ "^/users/\w+$")
{
return 301 https://$host$request_uri;
}
这篇关于使用负正则表达式重写nginx URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!