【问题标题】:nginx rewrite /forum and /forumsnginx 重写 /forum 和 /forums
【发布时间】:2015-09-12 06:23:37
【问题描述】:

我需要将 /forum 和 /forums 重定向到一个名为 /new 的新文件夹。这是为了捕捉 name/type-o 的变体,它不能用作别名指令。

我需要这样的:

/forum to /new
/forum/ to /new/
/forums to /new
/forums/ to /new/
/forum/blah to /new/blah
/forum/wee/blah.jpg to /new/wee/blah.jpg

我要拔头发了,我知道这很简单,但我尝试了十几个示例,但都无法理解。

使用:

location /forum {
    return 301 $scheme://$host/new;
}

仅适用于 /forum,例如,它不适用于 /forum/blah(需要转到 /new/blah)。

02:22 AM [atlantis]/etc/nginx/sites root # curl -I xxx/forum
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sat, 12 Sep 2015 06:22:06 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://xxx/new
Strict-Transport-Security: max-age=63072000
X-Content-Type-Options: nosniff

02:22 AM [atlantis]/etc/nginx/sites root # curl -I xxx/forum/blah
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sat, 12 Sep 2015 06:22:12 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://xxx/new
Strict-Transport-Security: max-age=63072000
X-Content-Type-Options: nosniff

【问题讨论】:

  • 为什么是 HTTP 301 而不是 Nginx 重写指令?
  • 我尝试了两种方式。将采取任何有效的方法

标签: redirect nginx rewrite


【解决方案1】:

试试这个:

location ~ ^/forums(.*)$ {    
    return 301 /new$1;    
}

location ~ ^/forum(.*)$ {    
    return 301 /new$1;    
}

并且“return”指令优于“rewrite”。

【讨论】:

  • 谢谢,我在发帖之前已经尝试过了,只是我在 /forums 上方有 /forum 并且出现了错误。现在我重新定位了它的工作原理!
猜你喜欢
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2013-09-22
  • 2014-07-15
相关资源
最近更新 更多