在 CentOS 上,Apache 的重定向通常通过 mod_rewrite 或 Redirect / RedirectMatch 指令来实现。下面按常见场景说明。
CentOS 一般使用 httpd:
httpd -M | grep rewrite
如果没有 rewrite_module,启用它:
sudo vi /etc/httpd/conf.modules.d/00-base.conf
确保有:
LoadModule rewrite_module modules/mod_rewrite.so
重启 Apache:
sudo systemctl restart httpd
<VirtualHost *:80>
ServerName old.example.com
Redirect 301 / http://new.example.com/
</VirtualHost>
Redirect 301 /old/ http://example.com/new/
<VirtualHost *:80>
ServerName example.com
Redirect 301 / https://example.com/
</VirtualHost>
在网站根目录或虚拟主机中:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>
如果使用
.htaccess,需确保AllowOverride All
编辑网站目录下的 .htaccess:
RewriteEngine On
# 旧页面重定向
RewriteRule ^oldpage\.html$ /newpage.html [R=301,L]
# 全站 http -> https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
sudo systemctl restart httpd
# 或
sudo systemctl reload httpd
AllowOverride 和 mod_rewrite如果你能说明 具体重定向需求(如:域名跳转、路径跳转、带参数跳转),我可以给你更精确的 CentOS Apache 配置示例。