在 CentOS 上设置“域名跳转”通常指 将一个域名访问重定向到另一个域名(如 http://a.com → http://b.com),常见做法是通过 Web 服务器(Nginx 或 Apache) 实现。下面分情况说明。
yum install -y nginx
systemctl enable nginx
systemctl start nginx
编辑 Nginx 配置文件(如 /etc/nginx/conf.d/redirect.conf):
server {
listen 80;
server_name old.com www.old.com;
return 301 http://new.com$request_uri;
}
server {
listen 443 ssl;
server_name old.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
return 301 https://new.com$request_uri;
}
nginx -t
systemctl reload nginx
yum install -y httpd
systemctl enable httpd
systemctl start httpd
vi /etc/httpd/conf/httpd.conf
确保有:
LoadModule rewrite_module modules/mod_rewrite.so
编辑 /etc/httpd/conf.d/redirect.conf:
<VirtualHost *:80>
ServerName old.com
ServerAlias www.old.com
Redirect 301 / http://new.com/
</VirtualHost>
或使用 rewrite:
<VirtualHost *:80>
ServerName old.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old.com [NC]
RewriteRule ^/(.*)$ http://new.com/$1 [L,R=301]
</VirtualHost>
systemctl restart httpd
CNAME:让 www.old.com 指向 new.comold.com如果你要的是 浏览器地址变成新域名,必须用 Web 服务器跳转。
| 类型 | 说明 |
|---|---|
| 301 | 永久跳转(SEO 友好) |
| 302 | 临时跳转 |
| 全站跳转 | 所有路径都跳 |
| 单页跳转 | 只跳某个 URL |
最常用做法(Nginx 301 全站跳转):
server {
listen 80;
server_name old.com www.old.com;
return 301 https://new.com$request_uri;
}
如果你能告诉我:
我可以给你更精确的配置。