温馨提示×

centos apache如何实现跳转

小樊
41
2026-09-24 05:50:14
栏目: 智能运维

在 CentOS 上通过 Apache 实现“跳转”(重定向),常见有几种场景和做法。下面按最常用方式给你说明,基本都能覆盖 90% 需求。


一、使用 .htaccess 实现跳转(最常用)

1. 开启 .htaccess 支持

编辑虚拟主机或主配置:

vi /etc/httpd/conf/httpd.conf

找到你的网站目录,类似:

<Directory "/var/www/html">
    AllowOverride None
</Directory>

改成:

AllowOverride All

然后重启 Apache:

systemctl restart httpd

2. 301 永久跳转(域名跳转)

例如:
http://old.com → http://new.com

在网站根目录创建 .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^old.com$ [NC]
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]

3. HTTP 跳转 HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

4. 访问某个页面跳转到新页面

Redirect 301 /old.html http://example.com/new.html

二、在 Apache 虚拟主机中配置跳转(推荐生产环境)

编辑虚拟主机配置文件:

vi /etc/httpd/conf.d/example.conf

HTTP → HTTPS 跳转示例

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

域名跳转示例

<VirtualHost *:80>
    ServerName old.com
    Redirect permanent / http://new.com/
</VirtualHost>

重启 Apache:

systemctl restart httpd

三、使用 mod_alias(简单跳转)

适合简单需求:

Redirect 301 / http://newdomain.com/

四、常见跳转类型说明

类型 说明
301 永久跳转(SEO 友好)
302 临时跳转
Rewrite 灵活、可带条件
Redirect 简单、直接

五、排查问题小提示

  1. 确认模块已启用:
httpd -M | grep rewrite
  1. 没生效?
  • 是否重启 Apache
  • AllowOverride 是否 All
  • 是否清空浏览器缓存

如果你能告诉我:

  • 是 域名跳转 / http→https / 旧页面→新页面
  • CentOS 版本(7 / 8 / Stream)
  • Apache 是否已配置虚拟主机

我可以直接给你可用的完整配置。

0 踩