【问题标题】:redirect /foo.html to /foo but not / to /index in nginx在 nginx 中将 /foo.html 重定向到 /foo 而不是 / 到 /index
【发布时间】:2012-12-07 08:41:55
【问题描述】:

我在磁盘上的文件有扩展名:index.htmla.html。我想请求http://example.com/a 加载/var/www/a.htmlhttp://example.com/ 加载/var/www/index.html。我希望任何其他 url 重定向到规范 url,所以 http://example.com/a.html 应该重定向到 http://example.com/a

我的配置如下:

rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
location / {
    root   /var/www;
    try_files $uri.html $uri $uri/ =404;
}

这会将/a.html 重定向到/a 并成功从磁盘加载a.html

$ curl -D- -s http://www.jefftk.com/food.html | grep ^Location
Location: http://www.jefftk.com/food
$ curl -s http://www.jefftk.com/food | grep ^Location

但它会将/ 发送到/index

$ curl -s -D- http://www.jefftk.com/pictures/ | grep ^Location
Location: http://www.jefftk.com/pictures/index
$ curl -s -D- http://www.jefftk.com | grep ^Location
Location: http://www.jefftk.com/index

如果我删除重写规则,它会停止从/a.html 重定向到/a,但也会停止将/ 发送到/index

$ curl -D- -s http://www.jefftk.com/food.html | grep ^Location
$ curl -D- -s http://www.jefftk.com/food | grep ^Location
$ curl -D- -s http://www.jefftk.com/ | grep ^Location
$ curl -D- -s http://www.jefftk.com/pictures/ | grep ^Location

为什么会发生这种情况?我可以同时将 nginx 用于我想要的两个东西(没有 .html 扩展名,在 url 中没有 index)吗?

【问题讨论】:

    标签: redirect nginx


    【解决方案1】:

    我认为您的重写规则可能是倒退的。也许只是这样(没有重写规则):

    location / {
        try_files $uri.html $uri $uri/ =404;
    }
    
    location = / {
        index index.html;
    }
    

    编辑版本:

    抱歉,我没有完全理解您的描述。我重读了几次并对此进行了测试,它可能接近您想要做的事情:

    location = / {
        try_files /index.html =404;
    }
    
    location = /index {
        return 301 $scheme://$host;
    }
    
    location ~* \.html$ {
        rewrite ^(.+)\.html$ $scheme://$host$1 permanent;
    }
    
    location / {
        try_files $uri.html $uri/ @backend;
    }
    
    location @backend {
        # rewrite or do whatever is default for your setup
        rewrite ^ /index.html last;
        // or return 404;
    }
    

    代码示例(修订版 3):

    我希望第三次是一种魅力。也许这会解决你的问题?

    # example.com/index gets redirected to example.com/
    
    location ~* ^(.*)/index$ {
        return 301 $scheme://$host$1/;
    }
    
    # example.com/foo/ loads example.com/foo/index.html
    
    location ~* ^(.*)/$ {
        try_files $1/index.html @backend;
    }
    
    # example.com/a.html gets redirected to example.com/a
    
    location ~* \.html$ {
        rewrite ^(.+)\.html$ $scheme://$host$1 permanent;
    }
    
    # anything else not processed by the above rules:
    # * example.com/a will load example.com/a.html
    # * or if that fails, example.com/a/index.html
    
    location / {
        try_files $uri.html $uri/index.html @backend;
    }
    
    # default handler
    # * return error or redirect to base index.html page, etc.
    
    location @backend {
        return 404;
    }
    

    【讨论】:

    • 不过,这不会将http://example.com/file.html 重定向到http://example.com/file
    • 对不起,我没有完全理解你的问题,但我重新阅读并测试了一些新规则。我已经修改了上面的代码以包含其他位置。
    • 这也不起作用:虽然 / 没有重定向到 /index,/foo 仍然重定向到 /foo/index
    • 好的,我已经更新了代码示例并发布了上面的编辑修订版(#3)。这行得通吗?
    【解决方案2】:

    你在寻找这样的东西吗:

    location / {
        try_files $uri.html $uri/index.html =404;
    }
    

    基本上这将首先尝试a.html 文件,如果失败则尝试index.html,最后显示404。另外请记住在编辑您的vhost 文件后restart nginx

    【讨论】:

      猜你喜欢
      • 2016-03-02
      • 2011-08-31
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 2018-01-05
      相关资源
      最近更新 更多