regex - nginx URL 重写使用负正则表达式?

标签 regex nginx

我正在尝试将请求重定向到 nginx 中的 https,除非它的格式为 HOST/ANY_STRING_OF_CHARS/END_OF_URI,例如:

http://host.org/about # 没有重定向

http://host.org/users/sign_in # 重定向到 https://host.org/users/sign_in

这显然在 Apache 中有效,但我不明白爆炸是如何工作的(如果它不起作用,请忽略):

RewriteRule !/([a-z]+)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

如何在 nginx 重写规则中执行此操作?这并没有像我希望的那样工作:

rewrite !/([a-z]+)$ https://$server_name$request_uri redirect;

这也不会进行重定向,以防我的逻辑倒退:

rewrite /([a-z]+)$ https://$server_name$request_uri redirect;

请帮忙?

最佳答案

向客户端发送永久重定向:

server {
  listen 80;
  rewrite ^(/users/\w+)$ https://$host$1 permanent;
  ...
}

对于否定匹配,您可以使用:

if ($request_uri !~ "^/users/\w+$")
{
  return 301 https://$host$request_uri;
}

关于regex - nginx URL 重写使用负正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159108/

相关文章:

php - 图片未显示在页面上-在新标签页中打开会显示index.php

ssl - nginx 配置不重定向 80-->443 尽管有效

docker - 无法访问 qbittorrent docker webui

regex - 需要帮助理解 perl 中使用\*{4} 的搜索代码

c# - 将字符串与其中的任何非 ASCII 字符匹配的 .Net 正则表达式

regex - 我如何使用 diff 和 sed

regex - 为什么匹配运算符的 "match-only-once"优化仅适用于 "?"分隔符?

javascript - 如何将 javascript 中的 url 与正则表达式匹配

php - 我可以使用 nginX+Webkit+PHP+SQLite 创建 Mac Appstore 应用程序吗?

tomcat 8在处理get请求时抛出400错误