Redirect Nginx从旧URL重定向到新URL

Redirect Nginx从旧URL重定向到新URL,redirect,nginx,Redirect,Nginx,我们正在为我们的属性搜索切换供应商,每个供应商的URL格式都略有不同。我们已经索引了40000多个URL,并希望用户被301重定向到新的URL URL中唯一的区别是从下划线切换到连字符,以及从/idx/切换到/property/ 以下是旧的URL: 以下是新的URL: 你知道如何在不知道40000多个URL中的每一个都是什么的情况下重定向所有这些URL吗 谢谢, Keith一种方法是使用perl子例程将下划线更改为连字符。您需要使用perl编译nginx,除非它尚未包含在内。这不是一个完全有效的

我们正在为我们的属性搜索切换供应商,每个供应商的URL格式都略有不同。我们已经索引了40000多个URL,并希望用户被301重定向到新的URL

URL中唯一的区别是从下划线切换到连字符,以及从/idx/切换到/property/

以下是旧的URL:

以下是新的URL:

你知道如何在不知道40000多个URL中的每一个都是什么的情况下重定向所有这些URL吗

谢谢,
Keith

一种方法是使用perl子例程将下划线更改为连字符。您需要使用perl编译nginx,除非它尚未包含在内。这不是一个完全有效的解决方案,但它可能会让您朝着正确的方向前进:

在nginx.conf中,在http部分中添加以下内容:

perl_modules  perl/lib;
perl_set $fix_uri 'sub {
        use File::Basename;
        my $req = shift;
        my $uri = $req->uri;
        $uri = basename($uri);
        # Do some magic here, probably more elegant than this
        $uri =~ s/idx/property/g;
        $uri =~ s/_/-/g;
        return $uri;
}';
然后在该位置可以调用子例程:

   location ~ "/idx/(.*" {
            set $redirect_path $fix_uri;
            rewrite . $redirect_path;
    }

一种方法是使用perl子例程将下划线更改为连字符。您需要使用perl编译nginx,除非它尚未包含在内。这不是一个完全有效的解决方案,但它可能会让您朝着正确的方向前进:

在nginx.conf中,在http部分中添加以下内容:

perl_modules  perl/lib;
perl_set $fix_uri 'sub {
        use File::Basename;
        my $req = shift;
        my $uri = $req->uri;
        $uri = basename($uri);
        # Do some magic here, probably more elegant than this
        $uri =~ s/idx/property/g;
        $uri =~ s/_/-/g;
        return $uri;
}';
然后在该位置可以调用子例程:

   location ~ "/idx/(.*" {
            set $redirect_path $fix_uri;
            rewrite . $redirect_path;
    }

我自己更喜欢ngx_lua模块

location /idx/ {
    rewrite_by_lua '
        local tempURI, n = ngx.re.gsub(ngx.var.uri, "_", "-")
        local newURI, m = ngx.re.sub(tempURI, "/idx", "/property", "i")
        return ngx.redirect(newURI, ngx.HTTP_MOVED_PERMANENTLY)
    ';
}
第一行(gsub)将所有“u1”更改为“-”

第二行(子行)将第一个“/idx”更改为“/property”


第三行很明显

我自己更喜欢ngx_lua模块

location /idx/ {
    rewrite_by_lua '
        local tempURI, n = ngx.re.gsub(ngx.var.uri, "_", "-")
        local newURI, m = ngx.re.sub(tempURI, "/idx", "/property", "i")
        return ngx.redirect(newURI, ngx.HTTP_MOVED_PERMANENTLY)
    ';
}
第一行(gsub)将所有“u1”更改为“-”

第二行(子行)将第一个“/idx”更改为“/property”

第三条线很明显