1. 重定向所有请求到特定网址

这将所有传入请求重定向域名给url http://anotherdomain.com/dir1/index.php,如下配置。

server {
    listen 192.168.1.100:80;
    server_name mydomain.com;
    return 301 http://anotherdomain.com/dir1/index.php;
}

2. 重定向所有请求其他域名

这将在域名中的所有传入的请求重定向到另一个域名(http://anotherdomain.com/)与相应的请求的URL和查询字符串。

server {
    listen 192.168.1.100:80;
    server_name mydomain.com;
    return 301 http://anotherdomain.com$request_uri;
}

3. 将请求重定向与特定协议

这将在域名中的所有传入的请求重定向到另一个域名(http://anotherdomain.com/)与相应的请求的URL和查询字符串。它也将使用重定向的URL相同的协议。

server {
    listen 192.168.1.100:80;
    server_name mydomain.com;
    return 301 $scheme://anotherdomain.com$request_uri;
}

做上述更改后,重新启动服务器NGINX重新加载新添加的配置。

如何让访问https://www.a.com/1.html

就自动301跳转到https://www.b.com/2.html

if ($request_uri = /1.html ) { return 301 https://www.b.com/2.html; }

4.NGINX举例

location / {
        rewrite ^/$ https://www.baidu.com/ permanent;
        rewrite ^/(.*)/$ https://www.baidu.com/$1/ permanent;
        rewrite ^/(.*)/(.*)/$ https://www.baidu.com/$1/$2/ permanent;
        rewrite ^/(.*)/(.*)/(.*)/$ https://www.baidu.com/$1/$2/$3/ permanent;
        rewrite ^/(.*)/([0-9]+)\.html$ https://www.baidu.com/$1/$2 permanent;
        rewrite ^/(.*)/(.*)/([0-9]+)\.html$ https://www.baidu.com/$1/$2/$3 permanent;
        rewrite ^/(.*)/(.*)/([0-9]+)\.html$ https://www.baidu.com/$1/$2/$3 permanent;
    }