如何仅重定向位于nginx Map中的URL

时间:2013-05-11 18:12:51

标签: redirect map nginx

我正在将博客从a.com移至b.com。现在,我想告诉Google /书签,所有博客帖子(大约100个)都已移至b.com。我只想重定向博客帖子而不是别的。

在阅读了nginx中的map模块后,我尝试了以下内容:

map_hash_bucket_size 128;
map $uri $new {
  /post http://b.com/post
  # (repeated for all 100 posts)
}

当我将以下行放在server块中时:

rewrite ^ $new redirect;

它会重定向所有100个帖子,但我网域上的所有其他网页都会出现错误:302 Found

这是我在config中的整个服务器块:

server {

    listen 80;
    server_name b.com;
    root /my/old/path/;
    index index.html;

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
           expires max;
           log_not_found off;
    }

    # 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 $uri @backend;
    }

    # default handler
    # * return error or redirect to base index.html page, etc.
    location @backend {
        try_files /404.html 404;
    }

    location ~ \.php$ {
            expires off;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors on;
            fastcgi_pass php;
    }

    # apply the rewrite map
    rewrite ^ $new redirect;
}

我认为地图会干扰try_files地理位置\的调用(我需要)。

2 个答案:

答案 0 :(得分:3)

我为您的问题找到了解决方案on serverfault

  

这可能是失败的,因为你试图重定向所有   请求,是否与地图中的某些内容匹配。

     

为防止这种情况发生,请先检查是否有匹配。

if ($new) {
    return 301 $new;
}

所以替换

rewrite ^ $new redirect;

if ($new) {
    return 301 $new;
}

你应该好好去

答案 1 :(得分:0)

我一直无法弄清楚如何使用map,因为它对我来说仍然是一个字符串模块。现在我只是将所有重写放在我的服务器块(在所有位置块之上)的列表中,这可能非常慢。现在该文件如下所示:

server {

    listen 80;
    server_name a.com;
    root /my/old/path/;
    index index.html;

    rewrite /post http://b.com/post
    # (x100)

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
           expires max;
           log_not_found off;
    }

    # 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 $uri @backend;
    }

    # default handler
    # * return error or redirect to base index.html page, etc.
    location @backend {
        try_files /404.html 404;
    }

    location ~ \.php$ {
            expires off;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors on;
            fastcgi_pass php;
    }

    # apply the rewrite map
    rewrite ^ $new redirect;
}