Apache Redirect when the page is SSL

2019-08-28 21:27发布

问题:

I have a redirect from http://example.com to http://www.example.com. now i want this redirect to work when the user is in https protocol. meaning to redirect from https://example.com to https://www.example.com. is it possible? if not, how do i redirect https://example.com/page to http://example.com or http://www.example.com

i hope i was clear

回答1:

Redirecting from https://example.com to https://www.example.com works in the same way as redirecting from http://example.com to http://www.example.com, except that you have to put the rewrite rules in your SSL-enabled VirtualHost in addition to your plain HTTP VirtualHost section.

Where you may get a problem is that, for this to work, you need the initial connection to https://example.com to work and be accepted by the browser. For this, you need your certificate to be valid for both www.example.com and example.com. This is typically done by using multiple Subject Alternative Names in your server certificate (you should be able to see this by looking at the details of your certificate). Some CAs issue certificates both with and without the www. prefix.