之前文章写过Nginx下如何设置跳转到https和www域名, 本篇说一下Apache下如何设置.
确认是否开启rewrite重写模块
配置文件一般位于/usr/local/apache/conf/httpd.conf
找到
#LoadModule rewrite_module modules/mod_rewrite.so
, 去掉#号
站点配置文件设置
注意Apache2.2和2.4版本写法不一样:
Apache 2.2
Order allow,deny Allow from allAllowOverride All
### Apache 2.4
Order allow,deny Require all grantedAllowOverride All
修改站点根目录.htaccess文件
http 301重定向到https, 并且不带www跳转到带www的域名
RewriteCond %{HTTP_HOST} ^ [NC] RewriteRule ^(.*)$ /$1 [L,R=301] RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ /$1 [L,R=301]RewriteEngine On
记得重启Apache