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

  • 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 – user68563 Aug 14 '16 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 – user68563 Aug 15 '16 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] – user68563 Aug 15 '16 at 5:04
1
# Force SSL via 301 Redirect...
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
  • 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. – Ian Mariano Aug 14 '16 at 14:04
  • Please read my second anwser above which starts with "correction" – user68563 Aug 15 '16 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>

  • 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 – user68563 Aug 21 '16 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.