重定向到https:// www但首先重定向到http:// www。[HOST [而不是https:// [HOST]

时间:2015-01-22 11:41:22

标签: apache .htaccess mod-rewrite

挑战:将用户重定向到https://而无需支付两个SSL证书,但仅限一个。

想象一下,您只有一个SSL证书,对于www.domain.com,您希望您的用户仅访问https://www.domain.com

用户通常会输入http://domain.com

您的第一步是将它们直接重定向到https://www

我可以通过代码实现这一点,但是我想通过.htaccess来实现,因为代码在具有不同主机名的开发/测试环境中运行,我希望apache能够处理重定向而不是编写解决方案

我在网上找到了这个例子:

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

问题在于它首先将您重定向到:

https://[HOST],然后到https://www.[HOST]

但是,用户会在https://[HOST]收到有关证书问题的警告,这是公平的,因为我没有为非www域购买证书。

任何想法如何最好编写.htaccess规则以满足您首次重定向到...的场景

http://www.[HOST],然后到https://www.[HOST]

......而不是......

https://[HOST],然后到https://www.[HOST]

非常感谢, 维拉德

0 个答案:

没有答案