Let’s say I have a website that is available over HTTPS, e.g. https://example.com/foo
. Consider the following .htaccess
:
RewriteBase /
RewriteEngine On
RewriteRule ^foo$ bar [R=301,L]
Now, when I visit https://example.com/foo
, it redirects to http://example.com/bar
, over HTTP instead of HTTPS.
How can I make it go to HTTPS other than using the full scheme + host for every RewriteRule
with the R
flag? That is, this would do what I’m after:
RewriteBase /
RewriteEngine On
RewriteRule ^foo$ https://example.com/bar [R=301,L]
However, my .htaccess
file contains many such RewriteRule
s, and it feels like there should be a way to tell Apache/mod_rewrite “rewrite to https://
by default”. What I’m really looking for is a directive to tell Apache the site is using HTTPS, similar to how one can declare the host name through ServerName foo.ext
. Is that possible?
I guess this is a similar question on Stack Overflow: How to convince Apache of the original client protocol
VirtualHost
for HTTP and HTTPS? Or are you using the sameVirtualHost
for both protocols?VirtualHost
.VirtualHost
for HTTP that simply redirects everything to HTTPS (a very simple task, you don't need mod-rewrite for that). And a separateVirtualHost
that only serves HTTPS. I have no idea if such a separation helps with the specific issue you are asking about though.VirtualHost
is HTTP-only. HTTPS is taken care of by the load balancer in this particular case. What I’m really looking for is a directive to tell Apache the site is using HTTPS, similar to how one can declare the host name throughServerName foo.ext
.