【问题标题】:Nginx Rewrite: change only index location?Nginx Rewrite:仅更改索引位置?
【发布时间】:2012-04-04 18:32:43
【问题描述】:

如果用户没有 URI,我如何才能将用户发送到新位置?我正在尝试跟随,但它不起作用......它总是将我发送到 /newlocation

rewrite ^/$ http://www.domain.com/newlocation permanent;
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;

所以基本上,我需要的是:

如果用户在浏览器 www.domain.org 上写,它会发送到 www.domain.com/newlocation 如果用户在浏览器 www.domain.org/something 上写,它会发送到 www.domain.com/something

谢谢!

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    我不确定为什么您当前的方法不起作用。 ^/$ 应该只匹配 /。也许它是当前配置的其他东西。这是一个可以做你想做的事情的服务器。

    server {
      server_name www.domain.org;
    
      # Only match requests for /
      location = / {
        rewrite ^ http://www.domain.com/newlocation permanent;
      }
    
      # Match everything else
      location / {
        rewrite ^ http://www.domain.com$request_uri? permanent;
      }
    }
    

    【讨论】:

    • 我花了一些时间才弄清楚第一个表达式中还有一个“=”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2023-04-04
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多