在 Nginx 中,可以使用以下配置来实现二级域名强制跳转到其他域名:

nginx
server {
listen 80;
server_name sub.example.com;
return 301 $scheme://example.com$request_uri;
}
这里的解释如下:
1. `server` 块定义了一个虚拟主机,监听 80 端口。
2. `server_name` 指定了要匹配的域名,在本例中是 `sub.example.com`。
3. `return 301` 表示返回一个 301 永久重定向响应。
4. `$scheme` 用于获取当前访问的协议(HTTP 或 HTTPS)。
5. `$request_uri` 用于获取当前访问的完整 URI。
6. 最终将重定向到 `http(s)://example.com/original-request-uri`。
如果你需要重定向到 HTTPS,可以将 `$scheme` 替换为 `https://`:
nginx
server {
listen 80;
server_name sub.example.com;
return 301 https://example.com$request_uri;
}
这样,当用户访问 `http://sub.example.com` 时,就会被永久重定向到 `https://example.com`。
需要注意的是,如果你有其他域名也需要进行跳转,可以在 Nginx 配置文件中添加更多的 `server` 块来实现。

查看详情

查看详情