2

How can I redirect "everything" to https://www.example.com?

Attention: without using rewrites, without using htaccess; only using redirects, because is the correct way, as stated in the Apache manual (I have root access to the server).

And "everything" means the other 3 alternatives: http://example.com, http://www.example.com, https://example.com

3
  • Well, adding this line to 000-default.conf: "Redirect permanent / https_//www.example.com/", I can redirect http_//example.com and https_//example.com. But, I can't figure out how to redirect https_//example.com to https_//www.example.com – user68563 1 min ago edit Aug 14, 2016 at 12:58
  • CORRECTION: Well, adding this line to 000-default.conf: "Redirect permanent / https_//www.example.com/", I can redirect http_//example.com and http_//www_example.com. But, I can't figure out how to redirect https_//example.com to https_//www.example.com Aug 15, 2016 at 3:59
  • I had to give up and go partially with mod rewrite and got it working like this: 000-default.conf added Redirect permanent / https://www.example.com/ + default-ssl.conf added: RewriteEngine On + RewriteCond %{HTTPS} off [OR] + RewriteCond %{HTTP_HOST} !=www.example.com [NC] + RewriteRule ^(.*) https://www.example.com/$1 [NS,L,R=permanent] Aug 15, 2016 at 5:04

2 Answers 2

1
# Force SSL via 301 Redirect...
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
2
  • If you can't do this in .htaccess or .conf, you can do it in code. Just do the redirect the same, if it's port 80, 301 redirect to https : //HOST. Aug 14, 2016 at 14:04
  • Please read my second anwser above which starts with "correction" Aug 15, 2016 at 4:00
0

This should work:

<VirtualHost *:80> ServerName www.example.com ServerAlias example.com Redirect permanent / https://www.example.com/ </VirtualHost> <VirtualHost *:443> ServerName example.com Redirect permanent / https://www.example.com/ </VirtualHost> <VirtualHost *:443> ServerName www.example.com ... <configs> ... </VirtualHost>

1
  • I've tried that but it did't work. I'm not using virtual hosts, but instead 000-default.conf e default-ssl.conf. Maybe, I've adapted wrong. Here's how I've done it in default-ssl.conf: ServerAlias example.com Redirect permanent / example.com ServerName www.example.com DocumentRoot /var/www Aug 21, 2016 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .