在 Debian 上用 Apache 做重定向,通常有以下几种常见方式。下面按使用场景给你说明,并附带示例。
sudo a2enmod rewrite
sudo systemctl restart apache2
编辑虚拟主机文件(如 /etc/apache2/sites-available/example.conf):
<VirtualHost *:80>
ServerName old.example.com
Redirect permanent / https://new.example.com/
</VirtualHost>
或 301 跳转到带 HTTPS 的:
Redirect 301 / https://new.example.com/
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
.htaccess 做重定向(适合目录级).htaccess在虚拟主机中:
<Directory /var/www/html>
AllowOverride All
</Directory>
然后重启 Apache。
.htaccess 重定向示例RewriteEngine On
RewriteCond %{HTTP_HOST} ^old.example.com$ [NC]
RewriteRule ^(.*)$ https://new.example.com/$1 [L,R=301]
Redirect 301 /old.html https://example.com/new.html
wwwRewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
wwwRewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
# 编辑站点配置
sudo nano /etc/apache2/sites-available/xxx.conf
# 启用站点
sudo a2ensite xxx.conf
# 测试配置
apache2ctl configtest
# 重启 Apache
sudo systemctl restart apache2
如果你能告诉我:
我可以给你更精确的配置。