【问题标题】:How to lets apache switch automatically between http and https如何让apache在http和https之间自动切换
【发布时间】:2015-02-18 12:23:58
【问题描述】:

我负责运行一些常规网站和一些 SSL 网站的 Apache Web 服务器。所有这些网站均供公司内部使用。所有网站都使用 VirtualHost 指令。 所以我有

- http://siteA.mycompany
- http://siteB.mycompany
- http://siteC.mycompany
- https://siteD.mycompany
- https://siteE.mycompany

一切运行顺利,但有些用户向我抱怨他们无法访问这些网站,因为有时他们必须输入 http,有时他们必须输入 https ...(管理员的工作有时很辛苦)。

我正在寻找解决这项工作的方法:如果请求来自 http 协议,则必须重定向定义为使用 https 请求的网站。反向执行相同的工作(https 到 http)。

另一种解释我在寻找什么的方式:

* user request http://siteD.mycompany
* but website is define as using https protocol
* user's web browser change his request to https://siteD.mycompany
* user is no more bothered anymore by the bad protocol problem

有什么技巧/好的做法来做这样的工作吗? 由于这个“问题”引起了很多网站的关注,因此解决方案可能是通用的。

【问题讨论】:

  • 用户访问http://siteD.mycompany时会发生什么?另外,您可以为 siteD.mycompany 配置 VirtualHost 吗?
  • @slash 当用户访问'siteD.mycompany'因为这个ServerName没有配置它会出错,它被“ErrorDocument 404”apache指令捕获,将他发送到自定义错误页面

标签: apache redirect https virtualhost


【解决方案1】:

请参阅此 Wiki 文章 here
您的每个虚拟主机配置都需要这些规则。考虑https://siteD.mycompany 的示例,如果访问http,您将需要以下重写规则以将用户重定向到您网站的https 版本。

<VirtualHost *:80 *:443>
ServerName siteD.mycompany
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [R=301,L]
</VirtualHost>

http://siteA.mycompany应用相同的逻辑,可以使用以下规则。

<VirtualHost *:80 *:443>
ServerName siteD.mycompany
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteRule ^/?(.*) http://%{HTTP_HOST}/$1 [R=301,L]
</VirtualHost>

要使这些规则起作用,您必须确保虚拟主机配置接受端口 80 和 443 上的连接。目前,您可能有严格的端口配置,因为当您访问 https://siteD.mycompany 时,404 是
祝你好运!

【讨论】:

  • 您的回答对我有帮助。谢谢。我把&lt;VirtualHost *:80 *:443&gt; ServerName mysite.mycompany.org:443 ServerAlias mysite.mycompany.org:80 DocumentRoot /data/www/mysite ... RewriteEngine on RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [R=301,L] ... &lt;/VirtualHost&gt; 做得很好
  • @tdaget :很高兴它有帮助!请考虑投票。
  • 糟糕的是,我的声誉没有达到能够投票的最低限度:-/
猜你喜欢
  • 2011-10-20
  • 1970-01-01
  • 2014-01-11
  • 2012-08-17
  • 2010-11-09
  • 1970-01-01
  • 2015-04-02
  • 2012-05-14
  • 1970-01-01
相关资源
最近更新 更多