重写httpd.conf和虚拟主机SSL的规则

时间:2012-05-08 00:26:44

标签: apache .htaccess mod-rewrite virtualhost httpd.conf

我在httpd.conf中有两个虚拟主机,一个用于端口443,另一个用于端口80:

<VirtualHost IPADDRESS:80>
</VirtualHost>

<VirtualHost IPADDRESS:443>
</VirtualHost>

现在我想将每个请求重定向到我的服务器以转到https://www.mysite.com/,但http://www.mysite.com/blog/除了我希望博客不是SSL。我应该在哪里放置RewriteRules,在哪个virtualHost指令中?我需要什么样的规则?

1 个答案:

答案 0 :(得分:7)

在端口80 VirtualHost中,规则会将非博客的所有内容重写为SSL。在443主机中,它会将博客请求重写为非ssl(如果你想强制它们回到非ssl)

<VirtualHost IPADDRESS:80>
  RewriteEngine On

  # Rewrite everything except the blog to SSL
  RewriteCond %{REQUEST_URI} !^/blog
  RewriteRule (.*) https://www.example.com/$1 [L,R,QSA]
</VirtualHost>

<VirtualHost IPADDRESS:443>
  RewriteEngine On

  # Rewrite the blog back to plain http
  # Leave this out if you don't care that https requests to the blog stay
  # on ssl
  RewriteRule ^(blog*) http://www.example.com/$1 [L,R,QSA]
</VirtualHost>