在有些生产环境下,需要将所有访问http的请求都转到https上,可以通过伪静态来实现
[root@NFS test2]# cat /etc/redhat-release
CentOS release 6.3 (Final)
[root@NFS test2]# uname -m
x86_64
1)开启网站的对伪静态的支持
修改主配置文件httpd.conf,将其中的 AllowOverride None 为 AllowOverride All
<Directory “/home/blog”>
??? Options FollowSymLinks
??? AllowOverride All
??? Order allow,deny
??? Allow from all
</Directory>
2)将下面的代码添加到httpd-vhosts.conf中的关于网站test.com的相关配置中
RewriteEngine On
RewriteCond %{HTTPS} !on [NC]
RewriteCond %{HTTP_USER_AGENT} !(baiduspider|soso|bing|sogou|yahoo|sohu-search|yodao|robozilla|msnbot|msie|feedburner) [NC]
RewriteRule (.*) https://test.com%{REQUEST_URI} [R=301,NC,L]
提示:
这段代码的意思是说:当检测到 HTTPS 未开启(!on?嘛)的时候,把请求重定向到 https:// 的对应 URI
目前搜索引擎中似乎只有 Google 能抓取 https:// 站点,而其他的搜索引擎都不抓取,So
加了一个判断条件,即判断访客的 User Agent 是否是搜索引擎或者 IE 用户,如果不是,那才跳转到 https:// 版的页面。注意,这里面是不包含 Google Bot(Google 的爬虫)的,在因为 Google 本身就是能抓取?https:// 站点的,没必要重定向
3)这个时候当你在浏览器中,输入test.com的时候,就会自动跳转到https://test.com