Apache配置不带www跳转到带www的域名地址

 利用 Apache 环境的 .htaccess 伪静态 301 跳转,达到访问不带 www 的网址时会自动跳转到带 www 的网址。

1、确认是否开启rewrite重写模块

配置文件一般位于/etc/httpd/config/httpd.conf

找到(没有就添加)去掉#号

#LoadModule rewrite_module modules/mod_rewrite.so

2、设置AllowOverride

默认网站目录是 /var/www/html的情况下,找到AllowOverride

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

3、修改.htaccess

(1)使用 vi 命令在网站的根目录新建一个 .htaccess 文件(如果有就编辑)

vim /var/www/html/.htaccess

(2)在里面添加如下内容:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^xxx.com [NC]
    RewriteRule ^(.*)$ https://www.xxx.com/$1 [L,R=301]
</IfModule>

xxx.com为你的域名地址

(3)保存后重启 Apache 服务器

systemctl start httpd.service #启动
systemctl stop httpd.service #停止
systemctl restart httpd.service #重启
posted @ 2022-05-13 17:34  猿了个码  阅读(479)  评论(0编辑  收藏  举报